2

I am struggling to get the "alternates" attribute to work in the Django sitemap framework.

class StaticViewSitemap(sitemaps.Sitemap):
    priority = 0.5
    changefreq = 'daily'
    i18n = True
    alternates = True

    def items(self):
        return ['index', 'about']

    def location(self, item):
        return reverse(item)

The documentation appears to suggest the above i.e. setting i18n and alternates to True. But when I do that, my sitemap is literally a plain test string like this:

http://127.0.0.1:8000/en/daily0.5http://127.0.0.1:8000/en/about/daily0.5http://127.0.0.1:8000/es/daily0.5http://127.0.0.1:8000/es/about/daily0.5

Whereas it should look like so:

<urlset xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9" xmlns: xhtml = "http://www.w3.org/1999/xhtml" >
    <url >
        <loc > http: // 127.0.0.1: 8000/</loc >
        <changefreq > daily < /changefreq >
        <priority > 0.5 < /priority >
    </url >
    <url >
        <loc > http: // 127.0.0.1: 8000/about/</loc >
        <changefreq > daily < /changefreq >
        <priority > 0.5 < /priority >
    </url >
</urlset >

There are two issues:

  1. It doesn't have the correct formatting. What am I doing wrong here?
  2. I don't want the 'en' language prefix for the default language. How do I remove this?
Chris
  • 183
  • 2
  • 11
  • Have you found a solution to the issue? Facing the same problem. – yagus Nov 14 '22 at 13:23
  • I thought I had but actually I was getting mixed up with another issue. I haven't fixed the sitemap issue and decided to simply stick with the URLs for my default language. Then in the html pages for those default URLs I have included hreflang alternate tags that point to the same URLs in the other languages. I figured Google etc would then pick up the other languages that way rather than via the sitemap. – Chris Nov 16 '22 at 13:35

2 Answers2

0

To disable the language prefix for default language for your website, you have to add 'prefix_default_language=False' in your urls.py file.

urlpatterns += i18n_patterns(
    path('', include('polls.urls')),
........
    prefix_default_language=False,
    ) 

also make sure that you add LANGUAGE_CODE = 'en' or default language code for your website in settings.py file of the project.

0

I suppose that you check your sitemap.xml file in browser. So, plain text is displayed because browser cannot render files with <xhtml:link .../> tags "normally".

"Normally" is what you used to see when open sitemap.xml in browser. But sitemap.xml is intended for robots and not for humans (as mentioned in related questions).

So, you need to open source code of this page (CTRL + U) and you will see what a server responses by this URL, not what a browser renders for you.

Compare two screenshots:

  1. Navigate to /sitemap.xml in browser enter image description here

  2. Open the source code of /sitemap.xml document, CTRL + U enter image description here

As you see this is a valid sitemap.xml with all alternate links for two languages (in my case there are ru and en). And I also used prefix_default_language=False in my root URLconf (as @shank-sharma suggested in the first answer)

Conclusion: Django generates valid XML file with alternate links. Don't worry about how it looks in browser.

You can read more about this "problem" in related question and even try to force it to display "normally", but Google and others won't accept your sitemap.xml:

hotenov
  • 911
  • 1
  • 11
  • 17