AttributeError:'AcceptInvite'对象没有属性'email'

时间:2016-04-08 20:45:51

标签: django forms unit-testing attributeerror

我正在我的Django项目中进行一些单元测试,并且收到错误 "AttributeError: 'SignUp' object has no attribute 'email'" 当我进行这个测试时。

def test_signup(self):
        response = self.c.post('/accounts/signup/', {'email': 'test@test.com', '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',    'support@humanlink.co')
            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中的数据应该有我发送的电子邮件,我的客户端的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()

1 个答案:

答案 0 :(得分:2)

您的代码会遇到多个错误。要解决您的问题,请在查看方法signup中创建表单,但不应该执行form.emailform.password,因为这不是django处理表单数据的方式。

其他相关问题,首先,您需要先调用form.is_valid(),然后才能从表单对象获取任何数据。即使这样,您也应该使用form.cleaned_data['email']来访问表单数据。

其次。你不应该那样做空检查。如果你把:

email = forms.EmailField(required=True)

django会自动为你验证空虚。

第三,在views.py方法中引发Exception不会让您的表单将消息返回到您想要的模板。如果您有自定义表单验证,则应该在表单类的clean方法中执行此操作。

请查看关于how to use form properly的django文档。