所以我在base.html上有一个注册,然后表单重定向到注册页面。
def signIn(request):
if request.user.is_authenticated():
context = {
"extendVar":"baseLoggedIn.html",
}
return HttpResponseRedirect("out")
if request.user.is_authenticated()==False:
# form=SignUpForm(request.POST or None)
if request.method=="POST":
email=request.POST.get('email','')
password = request.POST.get('password','')
user = auth.authenticate(username=email,password=password)
print(email)
print(password)
print(user)
if user is not None:
print("notNone")
auth.login(request,user)
return HttpResponseRedirect("out")
else:
print("None quàlol")
context={
"failure":"Password and e-mail did not match",
}
return HttpResponseRedirect("out") #redirects to base.html
context = {
"extendVar":"baseNotLoggedIn.html",
}
return render(request, "base.html",context)
唯一的问题是,当我这样做时,它重定向到主页,但主页没有任何错误,我无法找到一种方法来显示密码+电子邮件所做的错误不相符,因为我正在重定向。
答案 0 :(得分:0)
您需要将消息作为上下文的一部分传递,而HttpResponseRedirect
不会将任何上下文作为参数,因此您需要return render(request, "base.html", context)
而不是HttpResponseRedirect
。然后,您可以从"failure"
访问context
密钥。
另外,请保持代码清洁!
def sign_in(request):
if request.user.is_authenticated():
context = {
"extendVar":"baseLoggedIn.html",
}
return HttpResponseRedirect("out")
else:
if request.method == "POST":
form = SignUpForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = auth.authenticate(username=email, password=password)
print(email)
print(password)
print(user)
if user:
print("notNone")
auth.login(request, user)
return HttpResponseRedirect("out")
else:
print("None quàlol")
context={
"failure":"Password and e-mail did not match",
}
return render(request, "base.html", context)
else: # form invalid, pass as variable back to user with form errors
return render(request, "base.html", {'form': form}
context = {
"extendVar":"baseNotLoggedIn.html",
}
return render(request, "base.html",context)