authenticate不起作用,user = authenticate(username = username,password = password)返回None。 用户不在这里,因此没有任何作用
def homepage(request):
if request.method == "POST":
if "form2" in request.POST:
form = Userform(request.POST)
if form.is_valid():
user=form.save()
print(user.password)
login(request,user)
return redirect("main:dashboard")
else:
messages.error(request,'invalid entry')
return redirect("main:homepage")
elif "form1" in request.POST:
username=request.POST["email1"]
password=request.POST["password1"]
print(username)
print(password)
user=authenticate(username=username,password=password)
print(user)
if user is not None:
print("hello")
login(request,user)
return redirect("main:dashboard")
else:
messages.error(request,'user doesnot exist')
return redirect("main:homepage")
form = Userform
return render(request,
"index.html",{"form":form}
)
答案 0 :(得分:0)
似乎在request
中缺少authenticate()
。尝试添加并查看。
user=authenticate(request, username=username, password=password)
示例:
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth import authenticate, login
<<import LoginForm/UserForm>>
def user_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
user = authenticate(request,
username=cd['username'],
password=cd['password'])
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse('Authenticated Successfully')
else:
return HttpResponse('Disabled Account')
else:
return HttpResponse('Invalid login')
else:
form = LoginForm()
return render(request, 'account/auth/login.html', {'form': form})