2016-03-01 73 views
0

這裏是我的主要項目timecapture/urls.py內容:Django的重定向沒有反向匹配錯誤

from django.conf.urls import url,include 
from django.contrib import admin 
from django.core.urlresolvers import reverse_lazy 
from . import views 


urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^$',views.index,name='index'), 
    url(r'^login/$',views.auth_login,name='auth_login'), 
    url(r'^logout/$',views.auth_logout,name='auth_logout'), 
    url(r'^timesheet/',include('timesheet.urls'),name='timesheet') 
] 

這裏是主要的項目時間表/ urls.py內的應用程序:

from django.conf.urls import url,include 
from django.contrib import admin 
from django.http import HttpResponse 
from . import views 

urlpatterns = [ 
    url(r'^$',views.index), 
] 

我我無法重定向到'時間表'網址。我使用下面的命令:

return redirect('timesheet') 

但這工作:

return redirect('/timesheet/') 

確切錯誤是'在這裏輸入的代碼

django.core.urlresolvers.NoReverseMatch: Reverse for 'timesheet' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] 

順便說一句,我使用的Django最新的1.9.2與python 3.4

回答

2

您沒有一個名爲「時間表」的網址。你有一個包含這個名字,但不是意見。

從主urls.py中的include中移除name='timesheet',並將其添加到timesheet/urls.py中的索引url中。

+0

或者他可以添加名稱到像URL(r'^ $',views.index,name ='index'),',所以代碼將被重定向''timesheet:index')' – tug

+0

@拖那是一個很好的方法來做到這一點。我是一個新手。感謝你們倆。 – t0il3ts0ap

+2

@tug'url()'的'name'參數不會設置一個名稱空間。你需要'namespace'參數來include()'。 – knbk