仅将有记录的 URL 添加到站点地图

发布于 2024-10-05 09:06:31 字数 866 浏览 3 评论 0原文

我正在尝试添加一些自定义 URL,所有这些 URL 都链接到一个视图,并且传递的参数不同。例如,我将使用多个 URL 来提取具有特定品牌的所有记录,因此它将是 /vehicles/inventory/。如果要确保 URL 实际上返回一些记录,我想要做什么...只有那些确实返回的记录才需要转到站点地图。

以下是我为某一年的所有品牌完成课程的方法(但有些 URL 没有返回任何车辆,我想消除它)。

class YearMakeSitemap(Sitemap):
  priority = 1.0
  changefreq = 'weekly'

  def items(self):
    makes = query.order_by('manufacturer_popularity', 'manufacturer').values_list
            ('manufacturer', flat = True)
    years = query.order_by('year').values_list('year', flat = True)
    url = []

    for make in makes:
        for year in years:
            url.append('%s/%s' % (make, year))

    return url

  def lastmod(self, obj):
    return datetime.now()

  def location(self, obj):
    return '/vehicles/inventory/%s/' % obj

与我当前的情况相反,如何让站点地图文件仅包含返回至少一条记录的 URL?

I'm trying to add a few custom URLs, all linked to one view, with the parameters passed being different. As an example, I'll be having several URLs to pull up all records having a certain make, so it'll be <root_url>/vehicles/inventory/<make>. What I want to do if to ensure that the URLs actually return some records...only those that do will need to go to the sitemap.

Here's how I've done the class for all makes in a certain year (but some of the URLs don't have any vehicles being returned and I want to eliminate that).

class YearMakeSitemap(Sitemap):
  priority = 1.0
  changefreq = 'weekly'

  def items(self):
    makes = query.order_by('manufacturer_popularity', 'manufacturer').values_list
            ('manufacturer', flat = True)
    years = query.order_by('year').values_list('year', flat = True)
    url = []

    for make in makes:
        for year in years:
            url.append('%s/%s' % (make, year))

    return url

  def lastmod(self, obj):
    return datetime.now()

  def location(self, obj):
    return '/vehicles/inventory/%s/' % obj

How can have the sitemap file having only the URLs that return at least one record as opposed to my current situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2024-10-12 09:06:31

做出这样的改变怎么样?

for make in makes:
    for year in years:
        if query.filter(year=year,manufacturer=make).count() > 0:
            url.append('%s/%s' % (make, year))

但我认为更有效的答案是这样做:

query.order_by("manufacturer_popularity","manufacturer","year").values("manufacturer","year").distinct()

并迭代该列表。

How about making this change?

for make in makes:
    for year in years:
        if query.filter(year=year,manufacturer=make).count() > 0:
            url.append('%s/%s' % (make, year))

But I think a more efficient answer would be to do this:

query.order_by("manufacturer_popularity","manufacturer","year").values("manufacturer","year").distinct()

and iterate over that list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文