不知道最近发生了什么。我在网站上添加了一个联系表格(它有效)但是当我把它绑在我的base.html上时,我发现我的菜单在这个特定网站上没有工作(#global变量)(html看起来根本就没有菜单,只是围绕它的div)。它只在包含我的表单的contact_form.html中出错。知道为什么会这样吗?
class ContactForm(forms.Form):
name = forms.CharField(required=False, label='Your name')
email = forms.EmailField(label='E-mail address')
subject = forms.CharField(label='Subject')
phone = forms.CharField(required=False, label='Phone number')
message = forms.CharField(label='Your message')
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
message = cd['message']
# construct the message body from the form's cleaned data
body = """\
from: %s
name: %s
phone: %s
message: %s""" % (cd['email'], cd['name'], cd['phone'], cd['message'])
send_mail(
cd['subject'], #subject is here
body, #here is the message
settings.DEFAULT_FROM_EMAIL, # from
[settings.DEFAULT_FROM_EMAIL] # to
)
return HttpResponseRedirect('/contact/thanks/')
else:
form = ContactForm()
csrf_form = {'form': form}
csrf_form.update(csrf(request))
return render_to_response('contact_form.html',csrf_form)
答案 0 :(得分:0)
aganders3是对的。
我想我在这里找到了我的解决方案:https://docs.djangoproject.com/en/dev/ref/templates/api/
修复了上面的代码,看起来像是:
return render_to_response('contact_form.html',
csrf_form,
context_instance=RequestContext(request)
)
一切都按原样运作。