0

如果用户身份验证失败,我正在尝试实现一个简单的页面重定向到索引页面。

想象一下,我有两条路径:

www.xyz.com/app/
www.xyz.com/app/user

第一个渲染索引页面,第二个是成功登录的授权用户登陆页面。

在我的views.py文件中,我有

def index(request):
  return render(request, "app/index.html")

def userLogin(request):
  loginUser = authenticate(username = request.POST["username"], password = request.POST["password"])

  if loginUser is not None:
    if not loginUser.is_active:
      loginUser.is_active = True
      loginUser.save()

      login(request, loginUser)
      return HttpResponse(reverse(user))    
    else:
      return HttpResponse(simplejson.dumps({"login" : "failed"}))

def userLogout(request):
  logout(request)
  return HttpResponseRedirect(reverse(index))

urls.py

urlpatterns = patterns("",
  url(r'^$', views.index, name = "index"),
  url(r'^user/$', views.user, name = "user"),
  url(r'^registerNewUser/$', views.registerNewUser),
  url(r'^userLogin/$', views.userLogin),
  url(r'^user/userLogout/$', views.userLogout),
)

注销功能是通过 Ajax POST 请求访问的,在日志中,我看到

 [11/Jul/2013 09:08:00] "POST /app/user/userLogout/ HTTP/1.1" 302 0
 [11/Jul/2013 09:08:00] "GET /app/ HTTP/1.1" 200 5039

但索引页面未加载。但是,如果我刷新页面,则会加载索引页面,因为条件调用会在失败时request.user.is_authenticated()触发相同的HttpResponseRedirect调用和参数。

4

1 回答 1

0
If login fail i mean in else case return render(request, "app/index.html") instead
of return HttpResponse(simplejson.dumps({"login" : "failed"}))

**updated userLogin**

def userLogin(request):
    loginUser = authenticate(username = request.POST["username"],
    password = request.POST["password"])

if loginUser is not None:
if not loginUser.is_active:
  loginUser.is_active = True
  loginUser.save()

  login(request, loginUser)
  return HttpResponse(reverse(user))    
else:
  return render(request, "app/index.html")
于 2013-07-11T17:08:48.523 回答