0

当我单击按钮并且表单有效时,它只会刷新页面。我检查了 POST 数据,它是正确的。可能是什么问题?

我有一个具有相同设置的不同联系表,它可以工作。

forms.py 文件:

class ContactFormDebate(forms.Form):

subject = forms.CharField(max_length=100)
debate_title = forms.CharField(max_length=100)
debate_description = forms.CharField()
debate_scope = forms.CharField(max_length=200)
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)

视图.py:

def contactDebate(request):
if request.method == 'POST': # If the form has been submitted...
    form = ContactFormDebate(request.POST) # A form bound to the POST data
    if form.is_valid():
        subject = form.cleaned_data['subject']
        debate_title = form.cleaed_data['debate_title']
        debate_description = form.cleaned.data['debate_description']
        sender = form.cleaned_data['sender']
        cc_myself = form.cleaned_data['cc_myself']

        recipients = ['someemail@email.com']
        if cc_myself:
            recipients.append(sender)

        from django.core.mail import send_mail
        message = debate_title.append(debate_descripton)
        send_mail(subject, message, sender, recipients)
        return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
    form = ContactFormDebate() # An unbound form

return render(request, 'newdebate.html', {
    'form': form,
})

模板:

<h2 class="page-heading"><span>ПОДНЕСИ НОВА ДЕБАТА</span></h2>
<form action="." method="post">{% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
    {{ form.subject.errors }}
    <label for="id_subject">Наслов на пораката:</label>
    {{ form.subject }}
</div>
<div class="fieldWrapper">
    {{ form.debate_title.errors }}
    <label for="id_debate_title">Наслов на дебатата:</label>
    {{ form.debate_title }}
</div>
<div class="fieldWrapper">
    {{ form.debate_description.errors }}
    <label for="id_debate_description">Опис на дебатата:</label>
    {{ form.debate_description }}
</div>
<div class="fieldWrapper">
    {{ form.sender.errors }}
    <label for="id_sender">Email адреса:</label>
    {{ form.sender }}
</div>
<div class="fieldWrapper">
    {{ form.cc_myself.errors }}
    {{ form.cc_myself }}
    <label for="id_cc_myself" id="myself">Сакам да ми пристигне копија од пораката</label>
</div>
<p>
<div style="clear: both;"></div>
<input type="submit" value="Испрати" class="btn1 btn-primary1" /></p>

4

2 回答 2

4

debate_scope您的 HTML 中缺少一个字段。

此外,您在正确的情况下有拼写错误:cleaed_data, cleaned.data.

于 2012-06-27T13:12:52.663 回答
0

如果表单无效,则没有 else 条件;你应该补充一点。其次,你应该确保你的电子邮件设置是正确的,否则你会得到一个例外。

def contactDebate(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactFormDebate(request.POST) # A form bound to the POST data
        if form.is_valid():
            subject = form.cleaned_data['subject']
            debate_title = form.cleaed_data['debate_title']
            debate_description = form.cleaned.data['debate_description']
            sender = form.cleaned_data['sender']
            cc_myself = form.cleaned_data['cc_myself']

            recipients = ['someemail@email.com']
            if cc_myself:
                recipients.append(sender)

            from django.core.mail import send_mail
            message = debate_title.append(debate_descripton)
            send_mail(subject, message, sender, recipients, fail_silenty=True)
            return HttpResponseRedirect('/thanks/') # Redirect after POST
         else:
            return render(request,'newdebate.html',{'form':form})
    else:
         form = ContactFormDebate() # An unbound form

    return render(request, 'newdebate.html', {'form': form})
于 2012-06-27T13:05:30.137 回答