2014-09-02 49 views
0

讓我先說我已經閱讀了許多與此論壇有關的例外,並且仍然無法找到解決方案。不知道我錯過了什麼。在簡單的Django應用程序中的NoReverseMatch異常

這裏是我的應用程序的urls.py:

from django.conf.urls import patterns, url, include 
from django.core.urlresolvers import reverse 
urlpatterns = patterns('', 
     url(r'^$', PeopleList.as_view(), name='people_list'), 
    ) 

這是產生錯誤的模板代碼:

<a href="{% url 'people_list' %}">Index</a> 

我得到的錯誤是

Reverse for 'people_list' with arguments '()' 
and keyword arguments '{}' not found. 0 pattern(s) tried: [] 

我通過這個錯誤讀了很多帖子,並嘗試了幾件事情(重新啓動Web服務器,改爲雙引號/單引號/不含引號等)。但我無法看到錯誤。

此代碼複製自a CRUD example,我使用的是django 1.6.4。

視圖代碼是:

from django.views.generic.list import ListView 
from people.models import Category, Person 
from django.views.generic.edit import CreateView, UpdateView, DeleteView 
from people.forms import CategoryForm, PersonForm 
from django.views.generic.detail import DetailView 
from django.shortcuts import get_object_or_404 
from django.core.urlresolvers import reverse 

class PeopleList(PersonMixin, ListView): 
    template_name = 'people/object_list.html' 

實際上代碼是從所述鏈路:http://www.pythondiary.com/tutorials/simple-crud-app-django.html。但我使用django 1.6。

在變化,我做的是從

from django.conf.urls.defaults import patterns, url, include 

我改

from django.conf.urls import patterns, url, include 
+0

您可以分享您正在使用的視圖或表單代碼來將'people_list''傳遞給您的模板嗎? – br3w5 2014-09-02 08:26:19

+0

ssbrewster,通過添加代碼來回答你的問題。我也已經從我複製這個代碼的地方給出了教程的鏈接。 – mht 2014-09-02 08:37:34

+0

@mht,在settings.py中檢查你的'ROOT_URLCONF'。它對應於你的urls.py嗎? – stalk 2014-09-02 08:53:28

回答

1

在你的基地urls.py包括你的應用程序的命名空間的網址,因此在模板中,你應該使用命名空間太。在你的情況下: <a href="{% url 'people:people_list' %}">Index</a>

有關命名空間的更多信息:https://docs.djangoproject.com/en/1.6/topics/http/urls/#url-namespaces

+0

此外,人們應該確保在URL命名空間之後使用冒號,在這種情況下使用'people:'。我使用點('.')代替了相當多的時間調試。 – 2014-11-28 03:48:32

相關問題