Reputation: 7747
I'm trying to set up a contact form, and have temporarily tried to get it to display the fields that were submitted when the submit button is hit (same page). When I click submit, it reloads with the contact form showing. So, here's my view:
from website.forms import ContactForm
def contact(request):
Success = False
email = ""
title = ""
text = ""
if request.method == "POST":
contact_form = ContactForm(request.POST)
if contact_form.is_valid():
Success = True
email = contact_form.cleaned_data['email']
title = contact_form.cleaned_data['title']
text = contact_form.cleaned_data['text']
else:
contact_form = ContactForm()
ctx = {'contact_form':contact_form, 'email':email, 'title':title, 'text':text, 'success':success}
return render_to_response('website/contact.html', ctx, context_instance=RequestContext(request))
Here's forms.py:
from django import forms
class ContactForm(forms.Form):
email = forms.EmailField()
title = forms.CharField()
text = forms.CharField( widget=forms.Textarea )
And here's the template:
{% if success %}
<p>{{ email }}</p>
<p>{{ title }}</p>
<p>{{ text }}</p>
{% else %}
<form action"." method="POST">
{{ contact_form.as_p }}
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>
{% endif %}
It seems very simple, but what am I doing wrong?
Upvotes: 1
Views: 497
Reputation: 19618
I think that the problem is that Success != success... you have to type variables with the right case... check also the output of {{success}} (It should be "None" or anyway not a boolean)
Upvotes: 1