Python django中通过电子邮件和数据库密码进行用户身份验证
我是django的初学者。我正在从事一个项目,在该项目中,客户和公司都有自己的帐户。models.py是:Python django中通过电子邮件和数据库密码进行用户身份验证,python,django,authentication,login,Python,Django,Authentication,Login,我是django的初学者。我正在从事一个项目,在该项目中,客户和公司都有自己的帐户。models.py是: class Company_SignUp(models.Model): comp_name = models.CharField(_('Company Name'), max_length=30) email = models.EmailField(_('E-mail'), unique=True) raise forms.ValidationError("This em
class Company_SignUp(models.Model):
comp_name = models.CharField(_('Company Name'), max_length=30)
email = models.EmailField(_('E-mail'), unique=True)
raise forms.ValidationError("This email address already exists.")
password1 = models.CharField(_('Password'), max_length=128)
password2 = models.CharField(_('Confirm Password'), max_length=30)
def __unicode__(self):
return smart_unicode(self.comp_name)
class Customer_SignUp(models.Model):
cust_name = models.CharField(_('Customer Name'), max_length=30)
email = models.EmailField(_('E-mail'), unique=True)
password1 = models.CharField(_('Password'), max_length=128)
password2 = models.CharField(_('Confirm Password'), max_length=30)
def __unicode__(self):
return smart_unicode(self.cust_name)
我的forms.py是:
class Company(forms.ModelForm):
class Meta:
model = Company_SignUp
widgets = {
'password1': forms.PasswordInput(),
'password2': forms.PasswordInput(),
}
fields = ('email','password1','password2','comp_name')
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields did not match."))
elif len(self.cleaned_data['password1']) < 8:
raise forms.ValidationError(_("The password must be 8 characters long."))
return self.cleaned_data
class Customer(forms.ModelForm):
class Meta:
model = Customer_SignUp
widgets = {
'password1': forms.PasswordInput(),
'password2': forms.PasswordInput(),
}
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields did not match."))
elif len(self.cleaned_data['password1']) < 8:
raise forms.ValidationError(_("The password must be 8 characters long."))
return self.cleaned_data
但现在我无法使用超级用户凭据登录到管理页面。我应该怎么做型号 考虑从
django.contrib.auth.models
扩展User
模型,如下所示。如果不想这样做,请跳到下一节(身份验证)
用户
模型具有公共字段,例如用户名
、名字
、姓氏
、电子邮件
,等等。您只需指定模型可能具有的任何额外属性
Django文档建议扩展AbstractBaseUser
,这可能也适用于您
请在此处阅读更多信息:
认证
对于基于电子邮件的身份验证,您需要编写自己的身份验证后端:
一旦设置好,您需要接受电子邮件/密码并使用authenticate
和login
进行身份验证
from django.contrib.auth import authenticate, login
def my_view(request):
email = request.POST['email']
password = request.POST['password']
user = authenticate(email=email, password=password)
if user is not None:
if user.is_active:
login(request, user)
# Redirect to a success page.
else:
# Return a 'disabled account' error message
else:
# Return an 'invalid login' error message.
上面的代码片段来自文档,我已经对其进行了修改,以适合您的用例
有关Django中身份验证的更多信息:
from django.contrib.auth.models import User
class Customer(User):
# extra fields
from django.contrib.auth import authenticate, login
def my_view(request):
email = request.POST['email']
password = request.POST['password']
user = authenticate(email=email, password=password)
if user is not None:
if user.is_active:
login(request, user)
# Redirect to a success page.
else:
# Return a 'disabled account' error message
else:
# Return an 'invalid login' error message.