使用django-contact-form,我覆盖了表单类:
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>'
这有效,但我希望from_email是表单中提交的名称和电子邮件。尝试在表单中引用“self.email”,但它抱怨自我没有被定义。看起来它应该很简单,但我无法从文档中看出如何解决这个问题。
感谢。
答案 0 :(得分:2)
好的,我看了一下ContactForm类。我可以看到将from_email设置为表单中提交的电子邮件的最简单方法是覆盖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
希望能帮到你。