0

如何获取 App 的 URL 路径

def logout(request):
    auth.logout(request)
    # path defined for the app in the projects urls.py
    return HttpResponseRedirect('??????')

我知道我可以硬编码,但我怎样才能得到它?

urls.py 文件

urlpatterns = patterns('',
    url(r'^chat/',include('djangoChat.urls')),

djangoChat.urls.py 文件

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^login/$',views.login,name='login'),
    url(r'^logout/$',views.logout,name='logout')
)

我希望注销方法重定向到/chat路径,以便调用 views.index 方法

4

1 回答 1

1

要按名称反向查找 URL,请执行以下操作:

from django.core.urlresolvers import reverse

def logout(request):
    auth.logout(request)
    return HttpResponseRedirect(reverse('index'))
于 2013-09-04T01:10:20.317 回答