简体   繁体   中英

Django - Sitemap - Manually set absolute urls

I'm generating a sitemap but the website that this sitemap is for has a different URL routing and a different domain.

I thought that overriding location method will work but the problem is that Django automatically adds Site url before each 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

Is there a way to tell Django not to build the URL automatically?

I solved this by building a custom template tag. I use that tag to replace the URL in the Sitemap template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM