django-contact-form: How to use form submitter's email as the from_email
Using django-contact-form, I have overridden the form class:
from contact_form.forms import ContactForm
from django.conf import settings
class ContactFormPublic(ContactForm):
tuples = settings.WORKSHOP_ADMINS
recipient_list = [mail_tuple[1] for mail_tuple in tuples]
from_email = 'Joe Jones <joe@jones.org>'
That works, but I want the from_email to be that of the name and email submitted in the form itself. Tried referring to "self.email" in the f开发者_开发技巧orm, but it complains that self is not defined. Seems like it should be simple, but I'm not able to tell from the documentation how to solve this.
Thanks.
Ok, I took a look at the ContactForm class. The easiest way I can see to set the from_email to the email submitted in the form is to override ContactForm.get_messge_dict:
from contact_form.forms import ContactForm
from django.conf import settings
class ContactFormPublic(ContactForm):
tuples = settings.WORKSHOP_ADMINS
recipient_list = [mail_tuple[1] for mail_tuple in tuples]
def get_message_dict(self):
if not self.is_valid():
raise ValueError("Message cannot be sent from invalid contact form")
message_dict = {}
"""
I removed 'from_email' from the message_part tuple check and updated the
message_dict, setting the 'from_email' to the value of self.email
"""
for message_part in ('message', 'recipient_list', 'subject'):
attr = getattr(self, message_part)
message_dict[message_part] = callable(attr) and attr() or attr
message_dict.update({'from_email' : getattr(self, 'email')})
return message_dict
Hope that helps you out.
精彩评论