########## models.py ############# from django import newforms as forms from django.newforms.widgets import * from django.core.mail import send_mail, BadHeaderError # A simple contact form with four fields. class ContactForm(forms.Form): name = forms.CharField() email = forms.EmailField() topic = forms.CharField() message = forms.CharField(widget=Textarea()) ########## views.py ############## from django.http import HttpResponse from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from contacts.models import ContactForm from django.template import RequestContext, Context from django import newforms as forms from django.newforms.widgets import * from django.core.mail import send_mail, BadHeaderError def contactview(request): subject = request.POST.get('topic', '') message = request.POST.get('message', '') from_email = request.POST.get('email', '') if subject and message and from_email: try: send_mail(subject, message, from_email, ['change@this.com']) except BadHeaderError: return HttpResponse('Invalid header found.') return HttpResponseRedirect('/contact/thankyou/') else: return render_to_response('contacts.html', {'form': ContactForm()}) return render_to_response('contacts.html', {'form': ContactForm()}, RequestContext(request)) def thankyou(request): return render_to_response('thankyou.html') ########## settings.py ############## Under urlpatterns -- (r'^contact/thankyou/', 'contacts.views.thankyou'), (r'^contact/', 'contacts.views.contactview'), ########## contacts.html ############ {% extends "base.html" %} {% block fulltitle %}Contact me!{% block title %}{% endblock %}{% endblock %} {% block header %}
Thank you for contacting us.
We will attempt to get back to you within 48 hours
{% endblock %}