我正在django中创建登录表单。当我运行应用程序并填写用户名和密码字段时。该页面始终重定向到一个条件(用户名是否正确)。登录表格问题
的代码是:
def home(request):
if request.method == 'POST':
username = request.POST.get('user_name')
password = request.POST.get('password')
user = authenticate(user_name=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# success
return render_to_response('registration/main_page.html',{'form':login},context_instance=RequestContext(request))
else:
#user was not active
return render_to_response('registration/q.html')
else:
# not a valid user
return render_to_response('registration/home.html')
else:
# URL was accessed directly
return render_to_response('registration/w.html')
它总是重定向到home.html的
else:
#user was not active
return render_to_response('registration/home.html')
为什么会发生?
您需要评估IS_ACTIVE对于初学者来说,这不是一个模板,所以它应该是,user.is_active() –
你的代码示例混在一起。评论说#user没有激活,但是你正在重定向到主页。查看顶部代码,当用户无效时,重定向到主页。哪种情况是正确的? – Mikael
我猜这里传递的用户是不活动的 – Rakesh