Python 在django联系人表单中添加主题行
我一直在尝试在Django联系人表单中添加主题行(https://bitbucket.org/ubernostrum/django-contact-form/overview),但我没有这样的运气。该模块默认设置为从文本文件读取主题,但我希望用户能够将其写入表单。以下是我在forms.py中编辑的内容:Python 在django联系人表单中添加主题行,python,django,Python,Django,我一直在尝试在Django联系人表单中添加主题行(https://bitbucket.org/ubernostrum/django-contact-form/overview),但我没有这样的运气。该模块默认设置为从文本文件读取主题,但我希望用户能够将其写入表单。以下是我在forms.py中编辑的内容: def __init__(self, data=None, files=None, request=None, *args, **kwargs): if requ
def __init__(self, data=None, files=None, request=None, *args, **kwargs):
if request is None:
raise TypeError("Keyword argument 'request' must be supplied")
super(ContactForm, self).__init__(data=data, files=files, *args, **kwargs)
self.request = request
name = forms.CharField(max_length=100,
widget=forms.TextInput(attrs=attrs_dict),
label=u'Name')
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
maxlength=200)),label=u'Email')
subject = forms.CharField(max_length=100,
widget=forms.TextInput(attrs=attrs_dict),
label=u'Subject')
body = forms.CharField(widget=forms.Textarea(attrs=attrs_dict),
label=u'Message')
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS]
# subject_template_name = "contact_form/contact_form_subject.txt"
template_name = 'contact_form/contact_form.txt'
_context = None
def message(self):
"""
Renders the body of the message to a string.
"""
if callable(self.template_name):
template_name = self.template_name()
else:
template_name = self.template_name
return loader.render_to_string(template_name,
self.get_context())
def subject(self):
"""
Renders the subject of the message to a string.
"""
maxlength=200)),label=u'Email')
return self.subject
阅读代码时,文本文件(contact_form/contact_form_subject.txt)不仅仅是读入,而是呈现为模板,然后插入到结果中。主题模板通过完整上下文传递:
By default, this context includes:
* All of the validated values in the form, as variables of the
same names as their fields.
* The current ``Site`` object, as the variable ``site``.
* Any additional variables added by context processors (this
will be a ``RequestContext``).
通过使模板类似{{subject}}(如果表单字段名为“subject”),您应该能够非常轻松地引用subject模板中的表单值