Django 中的用户创建表单

User creation form in Django

我正在在线上 Djago class 并且没有人可以寻求帮助。如果能得到任何帮助和提示,我将不胜感激。

我正在学习创建表单 (login/singup)。

到目前为止我在 models.py

from django.db import models
from django.contrib import auth


class User(auth.models.User,auth.models.PermissionsMixin):

    def __str__(self):
        return "@{}".format(self.username)

forms.py

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm

class UserCreateForm(UserCreationForm):

    class Meta:
        fields = ('username','email','password1','password2')
        model = get_user_model()

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].label = 'Display Name'
        self.fields['email'].label = 'Email Address'

我想添加 addtional firelds 作为姓氏,但是当我在 class meta in fields (forms.py) 中添加 'surname' 时出现错误,但是在线培训说我可以轻松添加其他字段。你能告诉我我做错了什么以及我如何添加这个字段吗?

当我在 forms.py 中添加这些字段 'surname' 时:

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm

class UserCreateForm(UserCreationForm):

    class Meta:
        fields = ('username','email','surname','password1','password2')
        model = get_user_model()

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].label = 'Display Name'
        self.fields['surname'].label = 'Surname'
        self.fields['email'].label = 'Email Address'

它给出以下错误:

raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (surname) specified for User

views.py我有:

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView,TemplateView
from . import forms

class SignUp(CreateView):
    form_class = forms.UserCreateForm
    success_url = reverse_lazy('login')
    template_name = 'accounts/signup.html'

首先,您使用的是 get_user_model() 方法,但您使用的是自定义 User 模型。

在你的设置中设置 AUTH_USER_MODEL:

AUTH_USER_MODEL = 'app.User'

然后将 surname 字段添加到您的 User 模型:

class User(auth.models.User):
    surname = models.Charfield()

    def __str__(self):
        return "@{}".format(self.username)

运行 并应用迁移。然后您就可以将 surname 字段添加到您的表单 fields

class UserCreateForm(UserCreationForm):

    class Meta:
        fields = ('username','email','surname','password1','password2')
        model = get_user_model()

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].label = 'Display Name'
        self.fields['surname'].label = 'Surname'
        self.fields['email'].label = 'Email Address'

您需要对 forms.py 文件进行以下更改:-

from appname.models import USER 
from django.contrib.auth.forms import UserCreationForm

class UserCreateForm(UserCreationForm):

    class Meta:
        fields = ('username','email','surname','password1','password2')
        model = USER

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].label = 'Display Name'
        self.fields['surname'].label = 'Surname'
        self.fields['email'].label = 'Email Address'
# In your forms.py, you need to add new field 'surname'

For Example:-
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm

class UserCreateForm(UserCreationForm):
    surname = forms.CharField(max_length=20)

    class Meta:
        fields = ('username','email','surname','password1','password2')
        model = get_user_model()

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].label = 'Display Name'
        self.fields['surname'].label = 'Surname'
        self.fields['email'].label = 'Email Address'