2017-01-14 12 views
0

私はこのチュートリアル(https://hellowebapp.com/news/introduction-to-class-based-views/)に追従しようとしていると、このビューをした:私は、フォームを送信する際ジャンゴ:「ContactView」オブジェクトが属性「フォーム」を持っていない

class ContactView(FormView): 
    form_class = ContactForm 
    success_url = reverse_lazy('index') 
    template_name = 'app/contact.html' 

    def form_valid(self, form): 
     contact_name = self.form.cleaned_data['contact_name'] 
     contact_email = self.form.cleaned_data['contact_email'] 
     form_content = self.form.cleaned_data['content'] 

     template = get_template('contact_template.txt') 
     context = Context({ 
      'contact_name': contact_name, 
      'contact_email': contact_email, 
      'form_content': form_content 
     }) 
     content = template.render(context) 

     email = EmailMessage(
      'New contact form submission', 
      content, 
      'Your website ' + '', 
      ['[email protected]'], 
      headers={'Reply-To': contact_email} 
     ) 
     email.send() 
     return super(ContactView, self).form_valid(form) 

は、しかし、私は次のエラーを取得します: 'ContactView' object has no attribute 'form'

エラーがこの部分に関連すると思われる:

'形態' は、未解決の参照である
contact_name = self.form.cleaned_data['contact_name'] 
    contact_email = self.form.cleaned_data['contact_email'] 
    form_content = self.form.cleaned_data['content'] 

このエラーを解決するにはどうすればよいですか?どんな助けでも大歓迎です! 私はPython 3.5とDjango 1.9を使用しています。

答えて

3

self.formの代わりにform(フォームインスタンスがパラメータとして関数に渡されるため)である必要があります。

def form_valid(self, form): 
    contact_name = form.cleaned_data['contact_name'] 
    contact_email = form.cleaned_data['contact_email'] 
    form_content = form.cleaned_data['content'] 
    ... 
関連する問題