0

我正在编写一个包含用户注册和登录功能的应用程序。成功登录/注册操作后,用户将被重定向到user.html

现在,整个 HTML 文本显示为 URL 参数,这会引发错误 404

我用于视图处理的视图代码:

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

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

def userLogin(request):
  print "Loggin In!"
  loginUser = authenticate(username = request.POST["username"], password = request.POST["password"])
  print loginUser.username
  print loginUser.is_staff
  if loginUser is not None:
    if loginUser.is_active:
      login(request, loginUser)
      return HttpResponseRedirect(reverse(user))

和我的网址:

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

在重定向时,这是显示的消息:

Django tried these URL patterns, in this order:

^admin/
^app/ ^$ [name='index']
^app/ ^user/$
^app/ ^registerNewUser/$
^app/ ^userLogin/$


The current URL, app/<html><head><title>User Landing Page</title><link rel = "stylesheet" type = "text/css" href = "/static/css/jquery-ui.min.css" ><link rel = "stylesheet" type = "text/css" href = "/static/css/main.css"><script type = "text/javascript" src = "/static/js/jquery.js"></script><script type = "text/javascript" src = "/static/js/jquery-ui.min.js"></script><script type = "text/javascript" src = "/static/js/jquery.cookie.js"></script><script type = "text/javascript" src = "/static/js/csrfSetup.js"></script></head><body>User is logged</body></html>, didn't match any of these.

为了在成功登录时呈现 HTML,我需要做什么?

4

2 回答 2

1

请再次阅读 django 的文档和反向方法:

在你的意见.py

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

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

def userLogin(request):
    loginUser = authenticate(username = request.POST["username"], password = request.POST["password"])     
    if loginUser is not None:
      if loginUser.is_active:
        login(request, loginUser)
        return HttpResponseRedirect(reverse("user")) #See that! it's a url name!
    return HttpResponse(reverse("index")) # What happens if can't log in?

在你的 urls.py

urlpatterns = patterns("",
  url(r'^$', views.index, name = "index"),
  url(r'^user/$', views.user, name="user"), #Note the "user"!
  url(r'^registerNewUser/$', views.registerNewUser),
  url(r'^userLogin/$', views.userLogin),
)
于 2013-07-09T01:03:42.757 回答
0

这是对我有用的解决方案。它包括使用reverse()with HttpResponse()

具体来说,

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

def user(request):
  if request.user.is_authenticated:
    if not request.user.is_staff:
        return render(request, "app/user.html")
    else:
        return render(request, "app/admin.html")

  else: 
    return HttpResponse(reverse(index))

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)

    #Note HttpResponse and unquoted user method
    return HttpResponse(reverse(user)) 
else:
    return HttpResponse(simplejson.dumps({"login" : "failed"}))

结合以下urls.py配置

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

成功登录或注册后,重定向工作到相关页面。

于 2013-07-10T02:39:51.180 回答