After a little hacking around, I ended up with this: (remember that the
forms are inheriting this class, so the function has inherent access to
fields - something I forgot):

class EmailForm(forms.Form):
    def sendemail(self, fromAdd, toAdd, subject, keys=None, breaks=[],
ignore=[]):
        body = ''

        if not keys: keys = self.cleaned_data.keys()
        for k in keys:
            if k in ignore: continue
            body = '%s%s: %s\n' % (body, self.fields[k].label,
self.cleaned_data[k])
            if k in breaks: body = '%s\n' % body

        send_mail(subject, body, fromAdd, ['o...@thepcspy.com'],
fail_silently=False)

        return "Thank you. Your form has submitted sucessfully."

The breaks list allows me to stick a gap in after a block of fields. The
ignore list allows me to skip certain fields without having to specify all
of them.

I'm sure this is fairly specified but I hope it helps somebody else in the
future.


On Mon, May 18, 2009 at 5:43 PM, Aneesh <aneeshvkulka...@gmail.com> wrote:

>
> I don't think there's a simple way to do this (someone correct me if
> I'm wrong!).  The view that processes your form receives a POST (or
> GET) request containing only key-value pairs for the form fields.  It
> doesn't get passed any other information about the form (incl
> labels).  So, you'd have to lookup the labels separately.  One way to
> do this would be to create a hashtable mapping obscure field names to
> descriptive labels, and when you generate the email, do a bunch of
> lookups to convert the field names to labels.
>
> Aneesh
>
> On May 18, 11:14 am, Oli Warner <o...@thepcspy.com> wrote:
> > I've made a couple of forms that are basically supposed to dump their
> > content into an email and send that on. My forms both inherit this class:
> >
> > class EmailForm(forms.Form):
> >     def sendemail(self, fromAdd, toAdd, subject, keys=None):
> >         body = ''
> >
> >         if not keys: keys = self.cleaned_data.keys()
> >         for k in keys:
> >             body = '%s%s: %s\n' % (body, k, self.cleaned_data[k])
> >
> >         send_mail(subject, body, fromAdd, ['o...@....com'],
> > fail_silently=False)
> >
> >         return "Thank you. Your form has submitted sucessfully."
> >
> > If you're confused as to what *fields* is, it's just an override so I can
> > pass in a custom order of the field names.
> >
> > That results in an email like this:
> >
> > > field1: value
> > > field2: value
> > > etc
> >
> > All very good, but it's a little cryptic for one of my forms where there
> are
> > lots and lots of fields. Instead of using the raw field name, can I grab
> the
> > label?
> >
> > I also have some complex field types (multiple choice and a select).
> Rather
> > than the HTML value being used, can I pass out the nice name that gets
> shown
> > to the person filling the form out?
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to