gpt4 book ai didi

Django 站点地图 : Get only pages of the current website

转载 作者:行者123 更新时间:2023-12-02 06:58:08 24 4
gpt4 key购买 nike

我想使用 Django 站点地图创建站点特定站点地图。

我的想法是做这样的事情:

def items(self):
current_site = Site.objects.get_current(self.request)
return current_site.pages.filter(draft=False)

但我有两个问题:

  1. self.request未定义,有没有办法在Sitemap中获取真实的current_site
  2. 项目网址不正确,应以正确的 current_site 开头

我想,我必须将正确的查询集直接传递到我的站点地图,但我该怎么做?

谢谢

雷米

最佳答案

您可以通过这种方式传递站点特定的查询集。

def items(self):   
return YourModel.objects.by_site()

by_siteYourModel 的管理器函数,

def by_site(self, site=None, **kwargs):
"""Get all pages for this site"""
if not site:
site = Site.objects.get_current()

site = site.id

return self.filter(site__id__exact = site, **kwargs)

关于Django 站点地图 : Get only pages of the current website,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9817856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com