我使用的是默认的Djangoregistration
(版本 0.8)在我的项目中,用户只需输入他们的用户名、电子邮件和密码。但是,我希望用户也在注册页面上输入他们的名字和姓氏。我怎样才能轻松做到这一点?
您可以做的是覆盖默认的 UserRegisterForm 并创建一个覆盖它的新字段。
创建一个名为 forms.py 或其他名称的新文件。导入您的视图并使用此表单。
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
class UserRegisterForm(UserCreationForm):
username = forms.CharField(max_length = 100)
email = forms.EmailField(max_length = 100)
password1 = forms.CharField(widget = forms.PasswordInput() , max_length = 100)
password2 = forms.CharField(widget = forms.PasswordInput(), max_length = 100)
first = forms.CharField(max_length = 100 )
last = forms.CharField(max_length = 100)
class Meta :
fields = "__all__"
def clean_username(self):
username = self.cleaned_data.get("username")
if not username :
raise forms.ValidationError("UserName cannot be empty !")
try :
user = User.objects.get(username = username)
except :
user = None
if user :
raise forms.ValidationError("User with the username -: {} already exits ".format(username))
return username
def clean_first(self) :
first = self.cleaned_data.get("first")
if not first:
raise forms.ValidationError("Kindly enter your first name !")
def clean_last(self) :
last = self.cleaned_data.get("last")
if not last:
raise forms.ValidationError("Kindly enter your last name !")
# in what ever field you want to apply validations and authentications create a function named "clean_{field_name}" and get the data from the cleaned_data attribute .
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)