1

我正在尝试设置一个联系表单,并暂时尝试让它显示在点击提交按钮时提交的字段(同一页面)。当我单击提交时,它会重新加载显示的联系表单。所以,这是我的看法:

from website.forms import ContactForm

def contact(request):

    Success = False
    email = ""
    title = ""
    text = ""

    if request.method == "POST":    
        contact_form = ContactForm(request.POST)

        if contact_form.is_valid():
            Success = True
            email = contact_form.cleaned_data['email']
            title = contact_form.cleaned_data['title']
            text = contact_form.cleaned_data['text']

    else:
        contact_form = ContactForm()

    ctx = {'contact_form':contact_form, 'email':email, 'title':title, 'text':text, 'success':success}

    return render_to_response('website/contact.html', ctx, context_instance=RequestContext(request))

这是forms.py:

from django import forms

class ContactForm(forms.Form):
    email = forms.EmailField()
    title = forms.CharField()
    text = forms.CharField( widget=forms.Textarea )

这是模板:

{% if success %}

    <p>{{ email }}</p>
    <p>{{ title }}</p>
    <p>{{ text }}</p>

{% else %}

    <form action"." method="POST">

    {{ contact_form.as_p }}

    <input type="submit" value="Send">
    <input type="reset" value="Reset">

    </form>
{% endif %}

看起来很简单,但我做错了什么?

4

1 回答 1

1

我认为问题在于 Success != success...您必须使用正确的大小写键入变量...还要检查 {{success}} 的输出(它应该是“None”或者无论如何都不是布尔值)

于 2013-03-28T09:03:17.257 回答