我想在我的website.urls文件中显示我的sitemap.xml文件中的网址
通过此网址文件,我可以毫不费力地显示条款 隐私和其他
网址
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 :(得分:0)
support
网址是在此行中的网址文件中包含的应用中定义的:
url(r'^', include('website.urls')),
reverse('support')
使Django在主URL文件中查找名为'support'
的URL。要正确访问此网址,您需要使用正确的URL namespace,默认为您应用的名称。
换句话说,这是访问网址的正确方法:
return reverse('website:support')
这意味着您的location
视图必须获得'website:support'
而不是'support'
。
如果您希望命名空间不是'website'
,只需将您想要的值传递给include()
方法。
url(r'^', include('website.urls', namespace='another-name')),
然后,reverse
参数将是:
return reverse('another-name:support')