2016-12-09 74 views
1

我想顯示的網址從我的website.urls我的sitemap.xml文件文件NoReverseMatch在/sitemap.xml的Django與StaticViewSitemap從app.urls

從這個URL文件我沒有煩惱,以顯示條款隱私

網址

from django.conf.urls import include, url 
from django.contrib import admin 
from django.contrib.staticfiles import views 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
from django.conf import settings 
from django.conf.urls import * # NOQA 
from django.conf.urls.i18n import i18n_patterns 
from django.contrib.sitemaps.views import sitemap 
from .sitemaps import StaticViewSitemap 
from . import views 


sitemaps = { 
    'static': StaticViewSitemap, 
} 

urlpatterns = [ 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^', include('website.urls')), 
    url(r'^terms/$', views.terms, name='terms'), 
    url(r'^privacy/$', views.privacy, name='privacy'), 
    url(r'^cdg/$', views.cdg, name='cdg'), 
    url(r'^about/$', views.about, name='about'), 
    url(r'^icon/$', views.icon, name='icon'), 
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap') 
] 


# This is only needed when using runserver. 
if settings.DEBUG: 
    urlpatterns = patterns('', 
     url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA 
      {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), 
     ) + staticfiles_urlpatterns() + urlpatterns # NOQA 

但是,當我想從另一個URL文件的意見(這是一個應用程序的url文件位於子文件夾內)我有一個錯誤。

這裏是我的sitemap.py文件

from django.contrib import sitemaps 
from django.core.urlresolvers import reverse 

class StaticViewSitemap(sitemaps.Sitemap): 
    priority= 0.5 
    changefreq ='daily' 

    def items(self): 
     return ['terms','privacy', 'about', 'cdg','support'] 

    def location(self, item): 
     return reverse(item) 

這裏是website.url PY文件

from django.conf.urls import patterns, url 
from . import views 

app_name = 'website' 
urlpatterns = patterns('', 

    url(r'^support/$', views.support, name='support'), 
    url(r'^galerie/$', views.galerie, name='galerie'), 
    url(r'^showcase/$', views.showcase, name='showcase'), 
    url(r'^blog/$', views.blog, name='blog'), 
    ) 

我有這個錯誤:

NoReverseMatch at /sitemap.xml 
Reverse for 'support' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] 
Request Method: GET 
Request URL: http://localhost:8000/sitemap.xml 
Django Version: 1.9.9 
Exception Type: NoReverseMatch 
Exception Value:  
Reverse for 'support' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] 
Exception Location: /Users/R/Virtualenvs/p3-dj1_9/lib/python3.5/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 508 

任何幫助將不勝感激

回答

0

support URL在其中被列入您的網址,一個應用程序定義文件中這一行:

url(r'^', include('website.urls')), 

reverse('support')使得Django的尋找一個在你的主文件的URL命名'support' URL。要正確訪問此網址,您需要使用正確的URL namespace,默認爲您的應用名稱。

換句話說,這是訪問URL的正確方法:

return reverse('website:support') 

這意味着你的location視圖必須接受'website:support',而不是隻'support'

如果要命名空間不是'website',只需將所需的值傳遞給include()方法。

url(r'^', include('website.urls', namespace='another-name')), 

然後,reverse說法是:

return reverse('another-name:support')