所以我试图在我的视图中使用身份验证方法,但总是返回"不存在",我使用MD5哈希作为我的密码字段,所以我不# 39;不知道那是不是问题
forms.py
class LoginForm(forms.Form):
email = forms.EmailField()
password = forms.CharField(widget=forms.PasswordInput)
#This Method Hash the password
def clean_password(self):
clearPassNoHash = self.cleaned_data['password']
self.password = md5.new(clearPassNoHash).hexdigest()
return self.password
views.py
def auth_login(request):
args = {}
form = LoginForm(request.POST)
email = request.POST['email']
password = request.POST['password']
user = authenticate(email=email, password=password)
if user is not None:
login(request, user)
print("Exist")
else:
print("Does not exist")
我已尝试使用check_password()方法(在我的表单中)实际运行 但是我不知道为什么我在使用authenticate()
时遇到了麻烦-----------------------------的更新 ---------- ----------------------
Views.py
def auth_login(request):
args = {}
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
print("existe")
print user
else:
print user
args['form'] = form
return render(request, 'login/login.html', args)
forms.py
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
其他观察: 我在settings.py中有这个使用我的自定义模型
AUTH_PROFILE_MODULE = 'StudentUsers.StudentRegistration'
这是我添加到模型中的用户名字段:
class StudentRegistration(AbstractBaseUser, models.Model):
username = models.CharField(max_length = 25, null=False, default="", unique=True)
答案 0 :(得分:0)
您需要从表单的cleaning_data中获取电子邮件和密码,而不是直接从请求中获取。阅读文档中的cleaning_data属性的更多信息:https://docs.djangoproject.com/en/1.9/ref/forms/api/#django.forms.Form.cleaned_data
password = form.cleaned_data['password']
答案 1 :(得分:0)
您不应该自己散列密码值。这就是authenticate
已经做过的事情;所以实际上你是两次哈希。