When i finished enter my username and password, this error is blow up, he write that username and password didn't match, but all data is valide and true! how to fix it? How to check the password, if the password in the database is stored in encrypted form, and the supplied password in the form of a string!
当我输入我的用户名和密码时,这个错误被放大了,他写道用户名和密码不匹配,但是所有的数据都是有效的!如何修复它吗?如何检查密码,如果数据库中的密码是以加密的形式存储的,以及以字符串的形式提供的密码!
Thank you all for your help, I will look forward to your advice!
谢谢大家的帮助,期待您的建议!
forms.py
forms.py
class UserLogInForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Username"), error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
password = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password"))
def clean_username(self):
user = User.objects.get(username__iexact=self.cleaned_data['username'])
if user:
return self.cleaned_data['username']
else:
raise forms.ValidationError('This user does not exist!')
def clean(self):
username = self.cleaned_data['username']
password = self.cleaned_data['password']
user = User.objects.filter(username=username)
if user.count() == 1:
user = user.first()
if user.check_password(password):
raise forms.ValidationError("Incorrect password!")
return self.cleaned_data
else:
raise forms.ValidationError('This user does not exist!')
views.py
views.py
def login_view(request):
form = UserLogInForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data['username'],
password = form.cleaned_data['password'],
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
return redirect('accounts/login')
context = {'form':form}
return render(request, 'accounts/registration/login.html', context)
1 个解决方案
#1
1
You are raising the error when the check_password()
returns True.
当check_password()返回True时,将引发错误。
I suggest you may re-write the method something like this,
我建议你重新写一下这个方法,
def clean(self, *args, **kwargs):
username = self.cleaned_data.get("username")
password = self.cleaned_data.get("password")
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise forms.ValidationError("This user does not exist!")
if user and not user.check_password(password):
raise forms.ValidationError("Incorrect password!"):
if user and not user.is_active:
raise forms.ValidationError("This user is no longer active.")
return super(UserLogInForm, self).clean(*args, **kwargs)
Also, remove the trailing commas from these lines in your view,
另外,从视图中的这些行中删除尾逗号,
username = form.cleaned_data['username']
password = form.cleaned_data['password']
Due to the trailing commas, python returns a tuple rather than a string.
由于后面的逗号,python返回元组而不是字符串。
#1
1
You are raising the error when the check_password()
returns True.
当check_password()返回True时,将引发错误。
I suggest you may re-write the method something like this,
我建议你重新写一下这个方法,
def clean(self, *args, **kwargs):
username = self.cleaned_data.get("username")
password = self.cleaned_data.get("password")
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise forms.ValidationError("This user does not exist!")
if user and not user.check_password(password):
raise forms.ValidationError("Incorrect password!"):
if user and not user.is_active:
raise forms.ValidationError("This user is no longer active.")
return super(UserLogInForm, self).clean(*args, **kwargs)
Also, remove the trailing commas from these lines in your view,
另外,从视图中的这些行中删除尾逗号,
username = form.cleaned_data['username']
password = form.cleaned_data['password']
Due to the trailing commas, python returns a tuple rather than a string.
由于后面的逗号,python返回元组而不是字符串。