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()
Comments
Got something to say?
This was written on March, 13 2008 and is filed in code, django.
Our Products
Categories
- SEO
- accessiblity
- code
- company news
- django
- gondola
- open source
- portfolio
- presentation
- screencast
- software
- subversion
- trailmapping
- wordpress
Archives
- June, 2009
- April, 2009
- February, 2009
- December, 2008
- November, 2008
- September, 2008
- August, 2008
- July, 2008
Elsewhere
What we’ve been up to online
-
@37signals, seeing a number of 500 errors clicking around Basecamp right now http://skitch.com/t/u7e
Pete, 1 day, 15 hours ago -
Basecamp 500 Internal Server Error
Pete, 1 day, 15 hours ago -
Gmail broke plain text replies. Plz fix! http://bit.ly/43nd3q
Pete, 1 week ago -
Building an Open Source Consulting Company
Pete, 1 week, 3 days ago -
< 30% of applicants correctly followed the instructions. Should have added "attention to detail" & "ability to follow instructions" as reqs
Pete, 1 week, 6 days ago -
Django snippets: Sorl Thumbnail + Amazon S3
Pete, 1 week, 6 days ago -
Lincoln Loop is still looking for a Project Manager. Interested? http://authenticjobs.com/jobs/3688/
Pete, 2 weeks ago -
Use PERT technique for more accurate estimates
Taking a weighted average of the most pessimistic, most optimistic, and most likely estimates of a task to get a realistic estimate of the time it will take.
Pete, 2 weeks, 3 days ago -
Evidence Based Scheduling - Joel on Software
Interesting approach to software estimation.
Pete, 2 weeks, 3 days ago -
Less Wrong: Planning Fallacy
People are terrible planners/estimators and there is evidence to prove it.
Pete, 2 weeks, 3 days ago -
A reminder of how simple business can be when you don't make it complicated - (37signals)
Refreshing, especially after after spending 2 days wading through client contracts and work orders.
Pete, 3 weeks, 5 days ago -
We're looking for a part-time Project Mgr to help us juggle the workload. Interested? info+pm@lincolnloop.com
Pete, 4 weeks, 1 day ago -
pushed to master at lincolnloop/django-protected-files
Pete, 1 month ago -
@richleland do you have libjpeg installed?
Pete, 1 month ago -
I have seen the future and it is Google Wave http://wave.google.com
Pete, 1 month ago


See also http://code.google.com/p/django-contact-form/
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?
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.
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.