Opened 4 years ago

Closed 11 days ago

#27395 closed New feature (fixed)

Allow generating '<link rel="alternate ... hreflang=...>" for sitemaps'

Reported by: Hovi Owned by: Florian Demmer
Component: contrib.sitemaps Version: 1.10
Severity: Normal Keywords:
Cc: Hendy Irawan Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Hovi)


django sitemap supports i18n field, that enables generating language-aware sitemaps, here is doc:

Problem is, that it generates them just as each url separately with no relations. One of google recommendations is to mark same content in different languages using sitemap with xhtml:link element:

Would be cool to add possibility to generate these tags.

Attachments (1)

sitemap_hreflang.diff (2.3 KB) - added by Hovi 4 years ago.

Download all attachments as: .zip

Change History (22)

comment:1 Changed 4 years ago by Tim Graham

Is the idea to add something like a template tag that generates something like:

<link rel="alternate"
<link rel="alternate"
<link rel="alternate"
<link rel="alternate"

for use on a webpage? (If so, I'm not familiar enough with sitemaps code to say whether or not it's feasible.) Or does this involve some change to the sitemap itself?

comment:2 Changed 4 years ago by Hovi

Both ways actually make sense (in html page and sitemap) and code generating it in both cases would be very similar, but to answer your original question - this involves changes to sitemap itself yes.

comment:3 Changed 4 years ago by Tim Graham

Summary: Django sitemap and i18nAllow generating '<link rel="alternate ... hreflang=...>" for sitemaps'
Triage Stage: UnreviewedAccepted

What are those changes? Are you able to offer a patch?

comment:4 Changed 4 years ago by Hovi

I included simple patch to demonstrate what would require changing, by no means it's supposed to be final. There may be a little more work to avoid duplicities, but it does pretty much everything necessary.

comment:5 Changed 4 years ago by Hovi

Description: modified (diff)

Changed 4 years ago by Hovi

Attachment: sitemap_hreflang.diff added


comment:6 Changed 4 years ago by Nick Mavrakis

Based on Hovi's suggestion I have created a branch ( about this.

How may I proceed on this?

I would really like to see this as a built-in in Django.
Until now, I have created a custom template tag in my project which produces Google's <link rel="alternate" hreflang="..." ...> elements. I have submitted my sitemap to Google and it has successfully verified.

comment:7 Changed 4 years ago by Tim Graham

If you could find another sitemaps user to validate that the design makes sense, the next step would be to add tests and documentation, then send a pull request.

comment:8 Changed 3 years ago by Hendy Irawan

+1 for this :) I also need such functionality

comment:9 Changed 3 years ago by Hendy Irawan

Cc: Hendy Irawan added

comment:10 Changed 3 years ago by Hendy Irawan

Temporary workaround: (didn't work in Django 1.11 and Python 3)
My fork has been tested working in Django 1.11 and Python 3.6:

It uses this usage pattern:

class ArticleSitemap(RelAlternateHreflangSitemap):
    # If you want to serve the links on HTTPS.
    protocol = 'https'

    def alternate_hreflangs(self, obj):
       return [('en-us', obj.alternative_object_url),]

    def items(self):
       return FooItem._default_manager.all()
Last edited 3 years ago by Hendy Irawan (previous) (diff)

comment:11 Changed 2 years ago by Pierre Sassoulas


There is django-hreflang that seem to do it properly. I just adapted it for django 2.0. Maybe we could integrate some or all of its functionality directly into django ?

There is a lot of way to use it, see

But I think if its intregrated in django is should be used like this :

Add django.(...).AddHreflangToResponse to MIDDLEWARE_CLASSES

Then in template :

{% load hreflang %}
    {% hreflang_tags %}

Hope this help.

comment:12 Changed 2 years ago by Hendy Irawan

Now there are at least 3 methods to solve this problem.

I hope contrib.sitemaps is improved soon. @Tim Graham, there are now 3 sitemaps users here that need this functionality.
So the need is valid.

comment:13 Changed 10 months ago by Florian Demmer

Owner: changed from nobody to Florian Demmer
Status: newassigned

comment:14 Changed 10 months ago by Florian Demmer

Has patch: set

comment:15 Changed 10 months ago by Simon Charette

Needs tests: set
Patch needs improvement: set

comment:16 Changed 10 months ago by Florian Demmer

Needs tests: unset
Patch needs improvement: unset

comment:17 Changed 4 months ago by Carlton Gibson

Patch needs improvement: set

From discussion on the PR: a few new attributes are suggested that control the alternates rendering. One more is needed, probably alternates, defaulting to False than would enable the opting-in to the new features. (Existing sitemaps would be unchanged until folks turned on the flags.)

Other than that, PR looks pretty good.

comment:18 Changed 12 days ago by Florian Demmer

Patch needs improvement: unset

Pushed more recommended changes after review by David Smith; please consider merge again :)

comment:19 Changed 11 days ago by Carlton Gibson

Triage Stage: AcceptedReady for checkin

comment:20 Changed 11 days ago by Carlton Gibson <carlton@…>

In 50e1ccbb:

Refs #27395 -- Added XML namespace declaration to sitemap template.

As per Google recommendations:

comment:21 Changed 11 days ago by Carlton Gibson <carlton@…>

Resolution: fixed
Status: assignedclosed

In 16218c20:

Fixed #27395 -- Added sitemap 'alternates' generation.

Updated the sitemap generator and default template to optionally
include link elements with hreflang attribute to alternate language

Note: See TracTickets for help on using tickets.
Back to Top