問題描述
Django login() 身份驗證總是返回 None (Django login() authenticate always returns None)
我已經編寫了這段代碼,它正確地註冊了用戶,但是當涉及到登錄功能時,它只適用於我在數據庫中擁有的超級用戶。每次我嘗試使用註冊表單登錄我喜歡的用戶時,身份驗證返回無。
我正在讓它打印表單數據,並且可以確認提交的表單具有準確的信息,但是除非我登錄超級用戶,否則每次都無法通過身份驗證。
查看次數:
def register(request):
print(request.method)
if request.method == 'POST':
form = reg_form(request.POST)
print(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
email = form.cleaned_data.get('email')
phone = form.cleaned_data.get('phone')
first_name = form.cleaned_data.get('first_name')
last_name = form.cleaned_data.get('last_name')
user = Users.objects.create(
username=username, email=email,
password=password, phone=phone,first_name=first_name, last_name=last_name
)
try:
user = Users.objects.get(phone=form.cleaned_data.get('phone'))
print("User exists")
except Users.DoesNotExist:
print("DoesNotExist")
return render(request, 'register.html', {'form': reg_form})
login(request, user)
return render(request, 'profile.html')
else:
print(form.errors)
return render(request, 'register.html', {'form': reg_form})
else:
return render(request, 'register.html', {'form': reg_form})
def log_in(request):
if request.method == 'POST':
form = log_form(request.POST)
print(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(request, username=username, password=password)
print(user)
if user is not None:
print("user exists")
login(request, user)
return render(request, 'profile.html')
else:
print("user does not exist")
print(form.errors)
return render(request, 'login.html', {
'form2': log_form
})
else:
return render(request, 'login.html', {
'form2': log_form
})
else:
return render(request, 'login.html', {
'form2': log_form
})
註冊表單:
class reg_form(ModelForm):
class Meta:
model = Users
fields = '__all__'
widgets = { 'password': forms.PasswordInput(),
}
登錄表格:
class log_form(forms.Form):
username = forms.CharField(label='username', max_length=64)
password = forms.CharField(widget=forms.PasswordInput)
參考解法
方法 1:
The password should be hashed: in Django the passwords are not stored in a readable format, but it stores the hash of that password. This concept is discussed in the changing password section of the documentation.
This thus means that for your reg_form
, you save the object with:
class reg_form(ModelForm):
class Meta:
model = Users
fields = '__all__'
widgets = { 'password': forms.PasswordInput()}
def save(*args, **kwargs):
user = super().save(*args, **kwargs)
user.set_password(user.password)
return user
This is a very simple version of a more advanced form: the UserCreationForm
perhaps it makes more sense, like @iklinac says, to subclass from that form, and slightly alter it to your specific model and fields.
方法 2:
Please check if the user is actually getting created in the database or not if the user is not getting created then make following changes to your register method:
use 'Users.objects.create_user' method instead of 'Users.objects.create', please find below link for more details:
https://docs.djangoproject.com/en/3.2/topics/auth/default/
Also hash your password as Willem is suggesting.
(by beatmaister、Willem Van Onsem、JayantSeth)