【问题标题】:Django - Sitemap - Manually set absolute urlsDjango - 站点地图 - 手动设置绝对网址
【发布时间】:2021-05-17 11:24:31
【问题描述】:

我正在生成站点地图,但该站点地图所针对的网站具有不同的 URL 路由和不同的域。

我认为覆盖 location 方法会起作用,但问题是 Django 会自动在每个 url 之前添加 Site url。

http://example.comhttps://thewebsite.com...


<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url><loc>http://example.comhttps://thewebsite.com/article/123/slug</loc><lastmod>2021-05-10</lastmod>
         <changefreq>hourly</changefreq><priority>0.5</priority>
    </url>
</urlset>
class WebsiteSitemap(Sitemap):
    changefreq = "hourly"
    priority = 0.5

    def items(self) -> typing.List:
        items = []
        items.extend(Article.objects.home())
        return items

    def location(self, obj: typing.Union[Article]):
        return obj.website_full_url

    def lastmod(self, obj: typing.Union[Article]) -> datetime:
        return obj.modified

有没有办法告诉 Django 不要自动构建 URL?

【问题讨论】:

    标签: python django sitemap django-sitemaps


    【解决方案1】:

    我通过构建自定义模板标签解决了这个问题。我使用 tag 替换站点地图模板中的 URL

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      相关资源
      最近更新 更多