make contact form work

This commit is contained in:
Sanj 2011-11-01 03:51:14 +00:00
parent 58dc149e42
commit 1c80b5be5f
2 changed files with 34 additions and 2 deletions

View File

@ -7,12 +7,24 @@ from django.template import RequestContext
from django import forms from django import forms
from django.forms.widgets import Textarea from django.forms.widgets import Textarea
from django.utils.html import urlize from django.utils.html import urlize
from django.core.mail import send_mail
class ContactForm(forms.Form): class ContactForm(forms.Form):
name = forms.CharField(max_length=255) name = forms.CharField(max_length=255)
email = forms.EmailField() email = forms.EmailField()
message = forms.CharField(widget=Textarea(attrs={'cols': 10, 'rows': 6})) message = forms.CharField(widget=Textarea(attrs={'cols': 10, 'rows': 6}))
def save(self, fail_silently=False):
if not self.is_valid():
raise ValueError("Sorry, invalid contact form.")
# from_email = self.cleaned_data['from_email']
email = self.cleaned_data['email']
name = self.cleaned_data['name']
message = self.cleaned_data['message']
message_body = '%s wrote: \n\n %s' % (name, message,)
recipients = ['sanjaybhangar@gmail.com']
send_mail(fail_silently=fail_silently, from_email=email, message=message_body, recipient_list=recipients, subject="Contact on theatreforum.in")
''' '''
class Meta: class Meta:
widgets = { widgets = {
@ -35,8 +47,23 @@ def index(request):
})) }))
def contact(request): def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid:
form.save()
errors = 0
success = True
else:
errors = form.errors
success = False
else:
form = ContactForm()
success = False
errors = []
# form = ContactForm() # form = ContactForm()
return render_to_response("registration/contact.html", RequestContext(request, { return render_to_response("registration/contact.html", RequestContext(request, {
'form': ContactForm() 'form': ContactForm(),
'errors': errors,
'success': success
})) }))

View File

@ -1,9 +1,13 @@
{% extends 'registration/register_base.html' %} {% extends 'registration/register_base.html' %}
{% block reg_content %} {% block reg_content %}
{% if success %}
<p id="titleLogin">Thanks for your message.</p>
{% else %}
<p id="titleLogin">Write to us</p> <p id="titleLogin">Write to us</p>
<form action="" method="POST" id="formItf">
<form action="" method="POST" id="formItf">
{% csrf_token %}
<table> <table>
{{ form.as_table }} {{ form.as_table }}
@ -15,6 +19,7 @@
</td> </td>
</table> </table>
</form> </form>
{% endif %}
{% endblock %} {% endblock %}