django:简单的联系表格不起作用

时间:2013-03-28 08:56:25

标签: django django-forms

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

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 %}

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

1 个答案:

答案 0 :(得分:1)

我认为问题在于成功!=成功......你必须用正确的案例输入变量...同时检查{{success}}的输出(它应该是“无”或者不是布尔值)