将主题行添加到django-contact-form

时间:2011-11-07 17:53:50

标签: python django

我一直在尝试为Django-Contact-Form添加一个主题行(https://bitbucket.org/ubernostrum/django-contact-form/overview),但我没有运气。默认情况下,模块设置为从文本文件中读取主题,但我希望用户能够将其写入表单。这是我在forms.py中编辑的内容:

        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

1 个答案:

答案 0 :(得分:0)

读取代码here,文本文件(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”),您应该能够非常轻松地在主题模板中引用表单值。