问题
The Django documentation is very minimal and I can't seem to get this to work.
Currently I have 3 individual sitemaps, and I would like to create a sitemap index for them:
(r'^sitemap1\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps':sitemap1}),
(r'^sitemap2\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps':sitemap2}),
(r'^sitemap3\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps':sitemap3}),
The Django documentation mentions adding something along the lines of:
url(r'^sitemap-(?P<section>.+)\.xml$', views.sitemap, {'sitemaps': sitemaps},name='django.contrib.sitemaps.views.sitemap'),
What is "section" in this case? And how do I access this index file? Is it downloadable or is it accessible via a url?
Any help is greatly appreciated!
Edit Basically I would like to accomplish this in Django: https://support.google.com/webmasters/answer/75712
回答1:
If you included the app with a namespace (e.g. include('myapp.urls', namespace='myapp'), then you need to include the namespace when reversing, e.g. {% url 'myapp:my_url_name' %} or reverse('myapp:my_url_name').
url(r'^sitemap-(?P<section>.+)\.xml$', cache_page(86400)(views.sitemap), {'sitemaps': sitemaps}, name='sitemapsname'),
url(r'^sitemap\.xml$', cache_page(86400)(views.index), {'sitemaps': sitemaps, 'sitemap_url_name': 'posts:sitemapsname'}),
note: posts:sitemapsname is my app
see source code of Django: https://github.com/django/django/blob/master/django/contrib/sitemaps/views.py
回答2:
The "sections" are what you define in a dictionary, something like this:
urls.py
from .views import StaticSitemap, BlogSitemap, NewsSitemap
SITEMAPS = {
'blog': BlogSitemap,
'main': StaticSitemap,
'news': NewsSitemap,
}
These will show up as sitemap-blog.xml, sitemap-main.xml and sitemap-news.xml in your sitemap.xml index.
These are laid out in your views.py, for example:
class BlogSitemap(Sitemap):
changefreq = "never"
priority = 0.5
def items(self):
return BlogPage.objects.all()
def lastmod(self, obj):
return obj.date
def location(self, obj):
return obj.url
You don't need to access the index file, as there is a default in Django that will be used. It should just work automatically.
来源:https://stackoverflow.com/questions/41947135/how-do-you-create-a-sitemap-index-in-django