我正在 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')
为什么会发生?