Django FormMail Clone

Here’s a little nugget I just posted to Django Snippets. It emulates the behavior of an old Perl script I used way back when, FormMail.pl.

I often find myself needing to build a form whose contents get emailed to the site owner(s). This class let’s you call form.notify() on any form that is a subclass of it to have the fields ordered and sent in a plain text email to all users that are flagged as staff.

from django import newforms as forms
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.mail import send_mail

class FormMail(forms.Form):
    def notify(self):
        """
        Sends an email to all members of the staff with ordered list of fields
        and values for any form that subclasses FormMail
        """
        site_name = Site.objects.get_current().name
        form_name = self.__class__.__name__
        subject = '%s %s Submission' % (site_name, form_name)
        user_list = [u.email for u in 
                    User.objects.filter(is_staff=True).exclude(email="").order_by('id')]
        message = ""
        for k in self.base_fields.keyOrder:
            message = message + '%s: %s\n\n' % (self[k].label, self.cleaned_data[k])
        send_mail(subject, message, user_list[0], user_list)
        
## Example form
class ContactForm(FormMail):
    name = forms.CharField()
    phone = forms.CharField(required=False)
    email = forms.EmailField()
    comment = forms.CharField(widget=forms.Textarea())

## Example view code
form = ContactForm(request.POST)
if form.is_valid():
    form.notify()

Reader Comments

  • March 25, 2008 at 2:14 p.m. #
    James Bennett responded:

    See also http://code.google.com/p/django-contact-form/

  • March 25, 2008 at 2:30 p.m. #
    Pete commented:

    Thanks James, I didn’t notice your project until after I had already written my code.

    I haven’t dug into django-comment-form, what extra goodies does yours offer?

  • March 25, 2008 at 3:20 p.m. #
    Rob Hawkins chimed in with:

    I’m just a user of James’ contact-form, but what drew me to it was akismet. It’s a subclass(?) of the default ContactForm class. Just need to add your akismet key to settings.py and point the contact form to AkismetContactForm and voila.

  • March 25, 2008 at 9:10 p.m. #
    Brian Luft piped up:

    Whoops – I just broke out the contact form on my site into a separate app… based on a comment by James during his talk at PyCon… not realizing that this existed. He probably mentioned it but I was typing away.
    I like this implementation as well – very slick! Thanks for sharing.

Got something to say?





This was written on March, 13 2008 and is filed in code, django.

Thanks for checking in on our blog today. Want to learn more about Lincoln Loop? Visit our home page.

Categories

Archives

Elsewhere

What I’ve been up to online

  • #5247: patch6702.diff - Django Code - Trac
    08.08.04, 20:30 #
  • #5247: patch.diff - Django Code - Trac
    08.08.04, 20:30 #
  • Anybody want to split a hotel room for #DjangoCon?
    08.07.31, 9:42 #
  • Just got confirmation I'm on the invite list for #DjangoCon. Being a member does have its benefits. Couch surf or hotel?
    08.07.31, 9:02 #
  • Admin Image Widget
    08.07.31, 6:28 #
  • Follow @trailmapping for Trailmapping.com updates. Just pushed some slick jQuery into production tonight.
    08.07.31, 1:53 #
  • Munin :: com :: djangoproject.com :: trac_tickets
    Graphs of trac tickets for Django
    08.07.29, 12:00 #
  • Automating tests in Django | Eric's Site
    Generate tests by simply clicking through your development site.
    08.07.24, 9:23 #
  • Creator/updater fields for admin
    08.07.21, 16:16 #
  • fairview computing :: Django geography hacks
    Nice base for a reusable Locations app for Django
    08.07.10, 16:58 #
  • working with a designer
    Great tips on how to be a good client to your designer and get the most out of him at the same time.
    08.07.09, 23:45 #
  • Fluency Admin at deanjrobinson.com
    Elegant WordPress admin interface
    08.07.09, 16:43 #
  • Search Docs with Shortwave
    08.07.08, 5:00 #
  • django-chunks - Google Code
    Handy little project for managing CMS content snippets.
    08.07.07, 16:53 #
  • Norbauer Inc: The Boston Ruby on Rails Development Consulting Firm
    I love 1 page websites. Short, sweet and to-the-point.
    08.07.02, 9:21 #

Interested in working with us?
Fill out the form below or contact us at:

PO Box 774441
Steamboat Springs, CO
80477

970.879.8810
info@lincolnloop.com