Daniel Roy Greenfeld

Daniel Roy Greenfeld

About | Articles | Books | Jobs | News | Tags

Simple Django email form using CBV

Here's a simple FormView Class Based Views for Django. Here is a sample of how to do one as a simple email form. There is no CAPTCHA in this example, that's the topic of a future blog post.

This version requires the following packages pip installed into your virtualenv.

  • django-crispy-forms so we can do Python driven layouts.
  • django-floppyforms so we get HTML5 elements for free.

They also need to be added to your list of INSTALLED_APPS:


In myapp.forms.py:

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
import floppyforms as forms

class ContactForm(forms.Form):

    name = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    subject = forms.CharField(required=True)
    message = forms.CharField(widget=forms.Textarea)

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.add_input(Submit('submit', 'Submit'))
        super(ContactForm, self).__init__(*args, **kwargs)

In myapp.views.py:

from django.conf import settings
from django.core.mail import send_mail
from django.views.generic import FormView

from myapp.forms import ContactForm

class ContactFormView(FormView):

    form_class = ContactForm
    template_name = "myapp/email_form.html"
    success_url = '/email-sent/'

    def form_valid(self, form):
        message = "{name} / {email} said: ".format(
        message += "\n\n{0}".format(form.cleaned_data.get('message'))
        return super(ContactFormView, self).form_valid(form)

In templates/myapp/email_form.html:

{% extends 'base.html' %} {% load crispy_forms_tags %} {% block title %}Send an
email{% endblock %} {% block content %}
<div class="row">
  <div class="span6">
    <h1>Send an email</h1>
    {% crispy form form.helper %}
{% endblock %} {% block extrajs %}
<script src="{{ STATIC_URL }}js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
  $(function () {
{% endblock %}

Tomorrow's blog post

In tomorrow's post I'll show how to add CAPTCHA into your project to help reduce spam messages.

Want to learn more?

Check out the Django book I co-wrote, Two Scoops of Django: Best Practices for Django 1.11!

Tags: python django forms howto class-based-views
← Back to home