【问题标题】:How to create index for django sitemaps for over 50.000 urls如何为超过 50.000 个 url 的 django 站点地图创建索引
【发布时间】:2014-07-14 17:04:44
【问题描述】:

我有如下url配置

url(r'^sitemap\.xml$', index, {'sitemaps': 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/#creating-a-sitemap-index

【问题讨论】:

  • 你最后想好怎么做了吗?

标签: python django sitemap django-sitemaps


【解决方案1】:

试试这个

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage

然后

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)

您可以使用 sitemap\.xml?page=5 等 URL 访问站点地图

【讨论】:

  • 是的,我知道这一点,但它在文档中说它在创建索引后自己处理分页。我不确定我哪里出错了,以及如何创建索引。
  • 文档说You should create an index file if one of your sitemaps has more than 50,000 URLs. In this case, Django will automatically paginate the sitemap, and the index will reflect that.
  • 在这种情况下,请尝试在 is_visible 字段的表上添加数据库索引。您可以使用db_index=True 做到这一点。 IMO,这真的归结为数据库优化。您最终可能会查看您的查询并尝试在数据库端调整它们。
  • is_visible feild 已经是db_index=True,还是有这个问题
  • 尝试运行一些数据库诊断,在这种情况下。获取 django 尝试在数据库服务器上调用的查询,并在其上运行解释计划。
【解决方案2】:

我设置了 limit=5000 并解决了问题。

class ArticlesDetailSiteMap(Sitemap):
    changefreq = "daily"
    priority = 0.9
    limit = 5000

    def items(self):
        return Article.objects.filter(is_visible=True, date_published__lte=timezone.now())

它为所有以 5000 分页的文章创建了分页网址

【讨论】:

猜你喜欢
  • 2017-06-16
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
相关资源
最近更新 更多