1
ユーザーが自分のWebサイトにログインするための機能を作成しました。ただし、管理者アカウントでログインしている場合にのみ動作します。そうしないと、登録ユーザーが存在しないことが検出され、「これはユーザーが存在しません」と表示されます。ここでDjangoの管理者アカウントしかログインできません
コードです:
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
def clean(self, *args, **kwargs):
username = self.cleaned_data["username"]
password = self.cleaned_data["password"]
if username and password:
user = authenticate(username=username, password=password)
if not user:
raise forms.ValidationError("This is user does not exist")
if not user.check_password(password):
raise forms.ValidationError("Incorrect password")
if not user.is_active:
raise forms.ValidationError("This user is not longer active")
return super(LoginForm, self).clean(*args, *kwargs)
ログインビュー:任意の助け
def login_view(request):
form = LoginForm(request.POST or None)
title = "Log in"
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
login(request, user)
return redirect('../../index/')
return render(request, 'user/loginRegister.html', {'form': form, 'title': title})
ありがとう!
あなたはあなたのビュー機能は、同様に表示することはできますか。 –
@RajaSimon done –
どのように登録ユーザーを作成しましたか?パスワードがハッシュされるように 'set_password'を使いましたか? – Alasdair