2011-11-07 38 views
1

我一直在嘗試將主題行添加到Django-Contact-Form(https://bitbucket.org/ubernostrum/django-contact-form/overview),但我沒有這樣的運氣。該模塊默認設置爲從文本文件讀取主題,但我希望用戶能夠將其寫入表單。下面是我編輯在forms.py:向django-contact-form添加主題行

 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 

回答

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``). 

您應該能夠通過使模板類似{{主題}}(如果表單字段命名很容易引用主題模板中的形式價值「學科」)。