2016-04-08 9 views
0

イム私のDjangoプロジェクトにいくつかのユニットテストをやって、そして私はこのテストを実行すると、エラー
"AttributeError: 'SignUp' object has no attribute 'email'" を取得していますがありません。はAttributeError:「AcceptInvite」オブジェクトが属性[メール]

def test_signup(self): 
     response = self.c.post('/accounts/signup/', {'email': '[email protected]', 'password': 'test123', 'password_conf': 'test123', 
                'org_name': 'test org', 'org_username': 'test org username', 'invite': '4013'}) 
     code = response.status_code 
     self.assertTrue(code == 200) 

このテストでは、申し込みフォームが表示され、新しいアカウントが作成されます。

def signup(request): 
    # """Register a new account with a new org.""" 

    if request.method == "POST": 
     form = SignUp(request.POST) 

     if not form.email or not form.password: 
      raise Exception("Email and Password are required") 
     if form.password != form.password_conf: 
      raise Exception("Password does not match confirmation") 
     if not form.org_name or not form.org_username: 
      raise Exception('Organization name and username are required') 
     if not form.invite: 
      raise Exception('Invitation code is required') 

     if form.is_valid(): 
      cleaned_data = form.cleaned_data 

      email = cleaned_data['email'] 
      password = cleaned_data['password'] 
      org_name = cleaned_data['org_name'] 
      org_username = cleaned_data['org_username'] 
      invite_token = cleaned_data['invite'] 

      invitation = OrgInvite.objects.get(token=invite_token) 

      if invitation.used: 
       raise Exception("invitation code is invalid") 

      account = Account(email=email, password=password) 
      account.save() 

      org = Org(org_name=org_name, org_username=org_username) 
      org.save() 

      invitation.used = False 
      invitation.save() 

      login(request) 

      # Send Email 

      md = mandrill.Mandrill(settings.MANDRILL_API_KEY) 
      t = invite_token.replace(' ', '+') 
      url = "https://www.humanlink.co/verify/{}".format(t) 
      message = { 
       'global_merge_vars': [ 
        {'name': 'VERIFICATION_URL', 'content': url}, 
       ], 
       'to': [ 
        {'email': account.email}, 
       ], 
      } 
      message['from_name'] = message.get('from_name',  'Humanlink') 
      message['from_email'] = message.get('from_email', '[email protected]') 
      try: 
       md.messages.send_template(
        template_name='humanlink-welcome', message=message, 
        template_content=[], async=True) 
      except mandrill.Error as e: 
       logging.exception(e) 
       raise Exception('Unknown service exception') 

申し込みフォームは電子メールのフィールドを持ち、request.POST内のデータは、私は私のクライアントのポストメソッドは私のユニットテストで使用されているとそれを送信していた電子メールを持っていなければならないので、私は本当に、なぜそれがわかりませんそれでも「電子メール」属性はありません。

フォーム:

class SignUp(forms.Form): 
    email = forms.EmailField() 
    password = forms.CharField() 
    password_conf = forms.CharField() 
    org_name = forms.CharField() 
    org_username = forms.CharField() 
    invite = forms.CharField() 

答えて

2

あなたのコードは、複数のエラーに苦しんでいます。あなたの質問に対処するには、ビューメソッドsignupでフォームを作成していましたが、form.emailまたはform.passwordを使用しないでください。これはdjangoがフォームデータを処理する方法ではないからです。

その他の関連する問題は、まずフォームオブジェクトからデータを取得する前にform.is_valid()に電話する必要があります。それでも、フォームデータにアクセスするには、form.cleaned_data['email']を使用する必要があります。

次に。あなたはそのような空のチェックをしないでください。

email = forms.EmailField(required=True) 

djangoが自動的に空の状態を確認します。

第3に、views.pyメソッドで例外を発生させても、必要なテンプレートにメッセージを返すフォームが得られません。カスタムフォーム検証がある場合は、フォームクラスのcleanメソッドで行う必要があります。

django docについては、how to use form properlyをご確認ください。

+0

解決策は問題を解決しました。 Shangありがとうございます。 – JBT

関連する問題