亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

/accounts/signup/ Manager 處的 AttributeError 不可用;

/accounts/signup/ Manager 處的 AttributeError 不可用;

大話西游666 2022-12-20 14:50:51
我有一個自定義注冊表單SignupForm,用于檢查email自定義用戶對象是否存在CustomUser,如果存在則引發ValidationError。但是當我嘗試提出錯誤時,我得到了AttributeError at /accounts/signup/  Manager isn't available; 'auth.User' has been swapped for 'accounts.CustomUser'。這是我的代碼。forms.pyfrom django.contrib.auth.forms import UserCreationFormfrom django import formsfrom django.core.exceptions import ValidationErrorfrom django.contrib.auth import get_user_modelclass SignupForm(UserCreationForm):    def __init__(self, *args, **kwargs):        super(UserCreationForm, self).__init__(*args, **kwargs)    email = forms.CharField(        widget=forms.EmailInput(        attrs={            'class': 'input',            'placeholder': '[email protected]'        }    ))    ...    # other fields (username and password)    ...    def clean(self):       User = get_user_model()       email = self.cleaned_data.get('email')       if User.objects.filter(email=email).exists():            raise ValidationError("An account with this email exists.")       return self.cleaned_dataviews.pyfrom django.urls import reverse_lazyfrom django.views.generic import CreateViewfrom .forms import SignupFormfrom .models import CustomUser...# other views and imports...class CustomSignup(CreateView):    form_class = SignupForm    success_url = reverse_lazy('login')    template_name = 'registration/signup.html'models.pyfrom django.db import modelsfrom django.contrib.auth.models import AbstractUserclass CustomUser(AbstractUser):    email = models.EmailField(unique=True)    def __str__(self):        return self.usernamesettings.pyAUTH_USER_MODEL = "accounts.CustomUser"我錯過了什么?
查看完整描述

1 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

基本上你缺少 ModelForm Meta


試試這個


class SignupForm(UserCreationForm):


    def __init__(self, *args, **kwargs):

        super(UserCreationForm, self).__init__(*args, **kwargs)


    email = forms.CharField(

        widget=forms.EmailInput(

        attrs={

            'class': 'input',

            'placeholder': '[email protected]'

        }

    ))


    ...

    # other fields (username and password)

    ...


    def clean(self):

       User = get_user_model()

       email = self.cleaned_data.get('email')

       if User.objects.filter(email=email).exists():

            raise ValidationError("An account with this email exists.")

       return self.cleaned_data


   class Meta:

       model = get_user_model()

       fields = ('username', 'email')


查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號