AttributeError: 'AcceptInvite' object has no attribute 'email'

AttributeError: 'AcceptInvite' object has no attribute 'email'

我在我的 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 方法发送的电子邮件,所以我真的不确定为什么它仍然没有 'email' 属性。

表格:

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()

您的代码存在多个错误。为了解决你的问题,在你的视图方法 signup 中你创建了一个表单,但你不应该做 form.emailform.password 因为那不是 django 处理表单数据的方式。

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

其次。你不应该那样做空头支票。如果你输入:

email = forms.EmailField(required=True)

django 会自动为您验证是否为空。

第三,在 views.py 方法中引发异常不会让您的表单 return 将消息发送到您想要的模板。如果您有自定义表单验证,您应该在表单 class 的 clean 方法中进行。

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