如何在Django上为多语言站点创建站点地图

问题描述 投票:0回答:1

抱歉,我需要帮助。我的网站上有2种语言(英国)如何为网站制作两个单独的站点地图?它们在url中有一个前缀,并且每个都只包含仅与所选语言有关的url。例如,两条路线:/uk/sitemap.xml/en/sitemap.xml

我尝试这个:

from django.contrib.sitemaps import Sitemap
from beerpost.models import Beerpost
from django.urls import reverse

class BeerpostSitemap(Sitemap):
    i18n = True
    priority = 0.5
    changefreq = 'daily'

    def items(self):
        return Beerpost.objects.filter(is_active=True)

    def lastmod(self, obj):
        return obj.updated

但是我有1个站点地图,其中包含所有两种语言的链接。我尝试删除后

i18n = True

并且在url.py中添加了

from django.conf.urls.i18n import i18n_patterns
urlpatterns += i18n_patterns(
  path('sitemap.xml', sitemap, {'sitemaps': sitemaps},name='django.contrib.sitemaps.views.sitemap'),
)

[获得所需的信息后:站点地图中的单独路线,但是该路线不包含前缀(en或ua),如何在其中添加他?我有这个:/uk/sitemap.xml

<url>
<loc>
https://rate-beer.info/lvivske-lvivske-svitle-rozlivne/8831
</loc>
<lastmod>2019-09-19</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>

/ en / sitemap.xml

<url>
<loc>
https://rate-beer.info/lvivske-lvivske-light-draft-beer/8831
</loc>
<lastmod>2019-09-19</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>

如何使用“ / en /”获取此信息?/en/sitemap.xml

<url>
<loc>
https://rate-beer.info/en/lvivske-lvivske-light-draft-beer/8831
</loc>
<lastmod>2019-09-19</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
python-3.x sitemap django-2.2
1个回答
0
投票

我找到了决定:

from django.contrib.sitemaps import Sitemap
from beerpost.models import Beerpost


class BeerpostSitemap(Sitemap):
    priority = 0.5
    changefreq = 'daily'
    def items(self):
        beers = Beerpost.objects.filter(is_active=True)
        for b in beers:
            if  b.slug_uk:
                b.slug_uk = str('uk/')+b.slug_uk
            if b.slug_en:
                b.slug_en = str('en/')+b.slug_en
        return beers

    def lastmod(self, obj):
        return obj.updated

和站点地图现在在URL中带有前缀

© www.soinside.com 2019 - 2024. All rights reserved.