2011-07-09 65 views
5

錯誤:Django的:夾縫NoReverseMatch而呈現:反轉爲 '*' 與參數 '()' 和關鍵字參數 '{}' 未找到

Caught NoReverseMatch while rendering: Reverse for 'archive' with arguments '()' and keyword arguments '{}' not found. 

Template error 

In template /home/bravedick/Aptana Studio 3 Workspace/blog/templates/homepage/index.html, error at line 7 

線7:

6 <a href="{% url index %}">Index</a> 
7 <a href="{% url archive %}">Archive</a> 
8 <a href="{% url contacts %}">Contacts</a> 

主urls.py:

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

# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    # Examples: 
    (r'^$', include('blog.apps.homepage.urls')), 
    # url(r'^$', 'blog.views.home', name='home'), 
    # url(r'^blog/', include('blog.foo.urls')), 

    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
    url(r'^admin/', include(admin.site.urls)), 
) 

我urls.py:

from django.conf.urls.defaults import * 

urlpatterns = patterns('blog.apps.homepage.views', 
    url(r'^$', 'index', name='index'), 
    url(r'^about/$', 'about', name='about'), 
    url(r'^archive/$', 'archive', name='archive'), 
    url(r'^contacts/$', 'contacts', name='contacts'), 
) 

觀點:

from django.shortcuts import render_to_response 
from blog.apps.data.models import Entry 

def index(request): 
    entries = Entry.objects.published_entries().order_by('-id') 
    ctx = {'entries':entries} 
    return render_to_response("homepage/index.html", ctx) 

def about(request): 
    return render_to_response("homepage/about.html") 

def contacts(request): 
    return render_to_response("homepage/contacts.html") 

def archive(request): 
    return render_to_response("homepage/archive.html") 

回答

9

我可以看到你的主要URL配置一個迫在眉睫的問題。您有一個'$'符號,表示包含語句中的url結尾。

這行應爲:

(r'^', include('blog.apps.homepage.urls')), 

這裏的the documentation for include

另外檢查blog.apps.homepage.urls是一個有效的導入路徑。運行下面打開Django的外殼:

./manage.py shell 

然後鍵入:

from blog.apps.homepage import urls 

如果你得到一個導入錯誤,嘗試找出正確的導入路徑應該是什麼和使用,在您的包括聲明。

+0

你是對的。謝謝。 – bravedick

相關問題