如何使用电子邮件而不是用户名进行用户身份验证? [英] How to use email instead of username for user authentication?
本文介绍了如何使用电子邮件而不是用户名进行用户身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
在Django中接受电子邮件地址作为用户名
与Django一起提供的身份验证模型基于
用户名。
The authentication model provided along with Django is based on username.
如何更改基于电子邮件而不是
用户名的身份验证?
What to do to change the authentication based on email instead of username?
更具体的:
使用用户名验证,登录用户我们请执行以下操作:
With username authentication, to login user we do the following:
user = authenticate(name,password)
.......
login(request,user)
如果我们验证$ b $,请写上述语句b使用电子邮件?
What to write for the above statements if we are authenticating using email?
表单:
我打算写我的自己的表单显示字段
电子邮件,密码和验证。
这是正确的方法吗?
I am planning to write my own form which shows the fields email, password and the validation. Is this the correct approach?
推荐答案
class UserForm( forms.ModelForm ):
class Meta:
model= User
exclude= ('email',)
username = forms.EmailField(max_length=64,
help_text = "The person's email address.")
def clean_email( self ):
email= self.cleaned_data['username']
return email
class UserAdmin( admin.ModelAdmin ):
form= UserForm
list_display = ( 'email', 'first_name', 'last_name', 'is_staff' )
list_filter = ( 'is_staff', )
search_fields = ( 'email', )
admin.site.unregister( User )
admin.site.register( User, UserAdmin )
两个答案都不是我的。对另一个线程投票给业主的业力提升。我只是把它们复制到这里,让这个线程尽可能的完整。
这篇关于如何使用电子邮件而不是用户名进行用户身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文