Django'用户对象没有属性META'

时间:2019-11-14 18:39:33

标签: django django-forms django-authentication

(不好意思) 当我制作自定义表单(forms.py)

class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True, label='Email')
first_name = forms.CharField(required=True, label='First name', max_length=100)
last_name = forms.CharField(required=True, label='Last name', max_length=100)

class Meta(UserCreationForm.Meta):
    model = User
    fields = ["username", "first_name", "last_name", "email", "password1", "password2"]

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

    def clean_email(self):
        if User.objects.filter(email=self.cleaned_data['email']).exists():
            raise ValidationError(self.fields['email'].error_messages['exists'])
        return self.cleaned_data['email']

这是我的观点

def register(request):
if request.method == 'POST':
    form = UserRegisterForm(request.POST)
    if form.is_valid():
        form.save()
        username = form.cleaned_data['username']
        password = form.cleaned_data['password1']
        user = authenticate(username=username, password=password)
        user.save()
        login(user)
        return redirect('/index/')
else: form = UserRegisterForm()

def login(request):
context = dict()
return render(request, "dashboard/login.html", context)

def register(request):
if request.method == 'POST':
    form = UserRegisterForm(request.POST)
    if form.is_valid():
        form.save()
        username = form.cleaned_data['username']
        password = form.cleaned_data['password1']
        user = authenticate(username=username, password=password)
        user.save()
        login(user)
        return redirect('/index/')
else: form = UserRegisterForm()

context = {'form':form}
return render(request, 'dashboard/register.html', context)

def dashboard(request):

    return render(request, "dashboard/dashboard.html")

Django返回我“ / register /中的AttributeError “用户”对象没有属性“ META”” idk为什么? 我想保持顺序 该如何解决?

写错我的地方

2 个答案:

答案 0 :(得分:1)

删除元类中的arg 并将UserCreationForm更改为Forms.Form

  class UserRegisterForm(forms.Form):
email = forms.EmailField(required=True, label='Email')
first_name = forms.CharField(required=True, label='First name', max_length=100)
last_name = forms.CharField(required=True, label='Last name', max_length=100)

class Meta: # just like this
    model = User
    fields = ["username", "first_name", "last_name", "email", "password1", "password2"]

    def save(self, commit=True):
        user = super(forms.Form, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

    def clean_email(self):
        if User.objects.filter(email=self.cleaned_data['email']).exists():
            raise ValidationError(self.fields['email'].error_messages['exists'])
        return self.cleaned_data['email']

############################################################################

从form.py中删除def保存,然后在view.py中使其变得简单 像这个例子

            if obj.is_valid():
                x = Users.objects.get(u_id=request.session['user_id'])
                form.cleaned_data['userid'] = x
                form = obj.save(commit=False)
            form.save()

答案 1 :(得分:0)

因此,您需要删除“ Meta”类并进行订购设置

forms.py

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)
    first_name = forms.CharField(required=True, label='First name', max_length=100)
    last_name = forms.CharField(required=True, label='Last name', max_length=100)
    field_order = ["username", "first_name", "last_name", "email", "password1", 
    "password2"]

,您需要在表格中保存字段

views.py

def register(request):
if request.method == 'POST':
    form = UserRegisterForm(request.POST)
    if form.is_valid():
        instance = form.save() #form
        username = form.cleaned_data['username']
        password = form.cleaned_data['password1']
        email = request.POST.get('email') # getting email
        first_name = request.POST.get('first_name') # getting first name
        last_name = request.POST.get('last_name') # getting last name
        user = authenticate(username=username, password=password) #making user
        instance.email = email #saving email
        instance.first_name = first_name #saving email
        instance.last_name = last_name #saving email
        instance.save() #save form
        form.save_m2m() # save all
        return redirect('/login/')
        messages.success(request, f'You have been registred')
else: form = UserRegisterForm()

context = {'form':form}
return render(request, 'dashboard/register.html', context)