当我单击按钮并且表单有效时,它只刷新页面。我检查了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)
views.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>
答案 0 :(得分:4)
您在HTML中缺少debate_scope
字段。
此外,您有正确案例的拼写错误:cleaed_data
,cleaned.data
。
答案 1 :(得分:0)
如果表单无效,则您没有其他条件;你应该补充一点。其次,您应该确保您的电子邮件设置正确,否则您将获得例外。
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})