提交注册表格数据时,我始终收到以下错误

时间:2019-07-11 11:48:17

标签: django

我正在尝试使用DRF创建银行系统。我需要在用户填写注册详细信息后立即自动为其分配一个帐号。 除此之外,如果我没有正确的提问技巧,请原谅我。

我正在使用django --version 2.2.3

models.py

class AccountDetails(models.Model):
    GENDER_CHOICE = (
        ("M", "Male"),
        ("F", "Female"),
    )
    user = models.OneToOneField(
        User,
        related_name='account',
        on_delete=models.CASCADE,
    )
    account_no = models.AutoField(
        unique=True,
        primary_key=True,
        null=False,
        validators=[
            MinValueValidator(10000000),
            MaxValueValidator(99999999)
        ]
    )

views.py

def register_view(request):
    if request.user.is_authenticated:
        return redirect("home")
    else:
        user_form = UserRegistrationForm(
            request.POST or None,
        )
        account_form = AccountDetailsForm(
            request.POST or None,
            request.FILES or None
        )
        address_form = UserAddressForm(
            request.POST or None
        )

        if user_form.is_valid() and account_form.is_valid() and address_form.is_valid():
            user = user_form.save(commit=False)
            account_details = account_form.save(commit=False)
            address = address_form.save(commit=False)
            password = user_form.cleaned_data.get("password1")
            user.set_password(password)
            user.save()
            account_details.user = user
            account_details.save()
            address.user = user
            address.save()
            new_user = authenticate(
                account_no=user.account_no, password=password
            )
            login(
                request, new_user, backend='accounts.backends.AccountNoBackend'
            )
            messages.success(
                request,
                '''Thank You For Creating A Bank Account {}.
                Your Account Number is {}, Please use this number to login
                '''.format(new_user.full_name, new_user.account_no))

            return redirect("home")

        context = {
            "title": "Create a Bank Account",
            "user_form": user_form,
            "account_form": account_form,
            "address_form": address_form,
        }

        return render(request, "accounts/register_form.html", context)

以下是输出:

AttributeError at /accounts/register/
'AnonymousUser' object has no attribute '_meta'

错误指出: views.py:

#request, new_user, backend='accounts.backends.AccountNoBackend'

我希望将重定向到交易详细信息页面。

完整的追溯调用:

回溯(最近通话最近):   文件“ /home/hunter/Desktop/banking/venv/lib/python3.6/site-packages/django/core/handlers/exception.py”,第34行,位于内部     响应= get_response(请求)

文件“ /home/hunter/Desktop/banking/venv/lib/python3.6/site-packages/django/core/handlers/base.py”,行_get_response中的115行     响应= self.process_exception_by_middleware(e,request)

文件“ /home/hunter/Desktop/banking/venv/lib/python3.6/site-packages/django/core/handlers/base.py”,行_get_response中的113行     响应= wraped_callback(请求,* callback_args,** callback_kwargs)

register_view中第48行的文件“ /home/hunter/Desktop/banking/banking_system/accounts/views.py”     请求,new_user,backend ='accounts.backends.AccountNoBackend'

文件“ /home/hunter/Desktop/banking/venv/lib/python3.6/site-packages/django/contrib/auth/init.py”,第126行,登录     request.session [SESSION_KEY] = user._meta.pk.value_to_string(user)

文件“ /home/hunter/Desktop/banking/venv/lib/python3.6/site-packages/django/utils/functional.py”的第257行位于内部     返回func(self._wrapped,* args)

AttributeError:“ AnonymousUser”对象没有属性“ _meta” [11 / Jul / 2019 13:06:54]“ POST / accounts / register / HTTP / 1.1” 500 84970

0 个答案:

没有答案