我一定只是在这里忽略了一些东西,但是在剥离所有内容之后 - 我似乎无法让 Django 将 Form 或 ModelForm 呈现给我的模板。下面的代码:
#forms.py
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
#views.py
def index(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid(): # All validation rules pass
return HttpResponseRedirect('/thanks/')
else:
form = ContactForm() # An unbound form
return render_to_response('home/new_website.html',{
'form': form,
})
#new_website_html
<html>
<body>
<form method = "post" action = "">
{{ form.as_p }}
</form>
</body>
</html>