我正在上在线Djago课程,没有人可以寻求帮助。我将不胜感激得到任何帮助和提示。
我正在学习创建表单(登录/登录(。
到目前为止,我在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'
我想添加额外的 firelds 作为姓氏,但是当我在fields
(forms.py
( 中添加class meta
'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'
# 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'
首先,您使用的是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'