Django - Sitemap - Manually set absolute urls

382 views Asked by At

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?

1

There are 1 answers

0
Jamey Harris On

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