如何创建超过50,000个网址的django Sitemap的索引 [英] How to create index for django sitemaps for over 50.000 urls
问题描述
我有以下url配置
url(r'^ sitemap\.xml $',index,{' sitemaps'),
url(r'^ sitemap - (?P< section>。+)\.xml',cache_page(86400)(sitemap),{'sitemaps':sitemaps}),
和站点地图包含以下站点地图
class ArticlesDetailSiteMap(Sitemap):
changefreq =daily
priority = 0.9
def items(self):
return Article .objects.filter(is_visible = True,date_published__lte = timezone.now())
但还有更多超过50.000篇。所以我尝试 /sitemap-articles.xml
时会收到超时错误,因为它尝试获取所有的文章。
任何想法如何创建索引并使分页在这里工作,如下面的文档所述,
https://docs.djangoproject。 com / en / dev / ref / contrib / sitemaps /#creation-a-sitemap-index
尝试这个
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
pre>
然后
article_list = Article.objects.filter is_visible = True,date_published__lte = timezone.now())
paginator = Paginator(article_list,10)
page = request.GET.get('page')
try:
articles = paginator.page(page)
除了PageNotAnInteger:
articles = paginator.page(1)
除了EmptyPage:
articles = paginator。页(paginator.num_pages)
A您可以使用
sitemap\.xml?page = 5
之类的网址访问网站地图I have the following url configuration
url(r'^sitemap\.xml$', index, {'sitemaps': sitemaps}), url(r'^sitemap-(?P<section>.+)\.xml', cache_page(86400)(sitemap), {'sitemaps': sitemaps}),
and sitemaps include following sitemap
class ArticlesDetailSiteMap(Sitemap): changefreq = "daily" priority = 0.9 def items(self): return Article.objects.filter(is_visible=True, date_published__lte=timezone.now())
but there are more than 50.000 articles. So i get timeout error when i try
/sitemap-articles.xml
because it tries to get all the articles.Any ideas how should i create an index and make the pagination work here as it says in the documentation below,
https://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/#creating-a-sitemap-index
解决方案Try this
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
And then
article_list = Article.objects.filter(is_visible=True, date_published__lte=timezone.now()) paginator = Paginator(article_list, 10) page = request.GET.get('page') try: articles = paginator.page(page) except PageNotAnInteger: articles = paginator.page(1) except EmptyPage: articles = paginator.page(paginator.num_pages)
And you can access the site map using the URLs like
sitemap\.xml?page=5
这篇关于如何创建超过50,000个网址的django Sitemap的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!