I’ve started working on Django forms since not long ago. I’ve been letting forms generate by them self simply by writing :
{{ test_form.as_p }}
which worked successfuly.
I’ve then decided to work around to customize this template but for some reason the line :
{{ form.subject }}
doesn’t show a form. Did i have misunderstood the documentation ? I’ve also checked the HTML generated and it has kind of simply jumped over the {{ form.suject }}
Here is the code :
The template :
<form action="/contact/" method="post">
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="id_subject">Email subject:</label>
{{ form.subject }}
</div>
<div class="fieldWrapper">
{{ form.message.errors }}
<label for="id_message">Your message:</label>
{{ form.message }}
</div>
<div class="fieldWrapper">
{{ form.sender.errors }}
<label for="id_sender">Your email address:</label>
{{ form.sender }}
</div>
<p><input type="submit" value="Send message" /></p>
And the view associated :
def contact(request):
if request.method == 'POST':
form = contact_form(data=request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
sender = form.cleaned_data['sender']
recipents = ['grit.erlum@gmail.com']
send_mail(subject, message, sender, recipents)
print 'mail sent'
return render_to_response('contact.html', {'contact_form' : contact_form}, context_instance=RequestContext(request))
And of course the Form Class :
class contact_form(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField(max_length=1000, widget=forms.Textarea)
sender = forms.EmailField()
Your view does not have
contact_formvariable defined, but you are passing that torender_to_response.You also need to create an unbound version of your form and pass it to
render_to_responselike: