多语言sitemap,get_absolute_url和位置

发布于 2025-01-26 02:05:39 字数 1386 浏览 3 评论 0原文

PLZ帮助我创造了正确的站点地图。 我在Django 2.2上具有标准国际化框架的多语言网站。

使用get_absolute_url

class Data(models.Model):
   ...
    def get_absolute_url(self):
        from django.urls import reverse
        return reverse("data_detail", kwargs={"slug": str(self.id)})


sitemap.py

class DataSitemap (Sitemap):
    changefreq = "daily"
    priority = 0.5
    i18n = True

    def items(self):
        return Data.objects.all()

    def location(self, obj):
        return '/news/data/%s/' % (obj.pk)

url.py

from django.contrib.sitemaps.views import sitemap
from .sitemaps import DataSitemap

sitemaps = {
    'data'   : DataSitemap
}

urlpatterns = i18n_patterns(
   path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, 
   name='django.contrib.sitemaps.views'),
)

现在,现在我没有语言前缀时,

<url>
   <loc>example.com/news/data/1/</loc>
     <lastmod>2022-03-24</lastmod>
     <changefreq>daily</changefreq>
   <priority>0.5</priority>
</url>
<url>
   <loc>example.com/news/data/1/</loc>
     <lastmod>2022-01-08</lastmod>
     <changefreq>daily</changefreq>
   <priority>0.5</priority>
</url>

现在没有get_absolute_url,但没有harcoded的位置,但一切正常,语言前缀均正确。

如何修复我的站点码?

Plz help me with the correct Sitemap generation.
My multilanguage site on Django 2.2 with standard internationalization framework.

Model.py with get_absolute_url

class Data(models.Model):
   ...
    def get_absolute_url(self):
        from django.urls import reverse
        return reverse("data_detail", kwargs={"slug": str(self.id)})


Sitemap.py

class DataSitemap (Sitemap):
    changefreq = "daily"
    priority = 0.5
    i18n = True

    def items(self):
        return Data.objects.all()

    def location(self, obj):
        return '/news/data/%s/' % (obj.pk)

url.py

from django.contrib.sitemaps.views import sitemap
from .sitemaps import DataSitemap

sitemaps = {
    'data'   : DataSitemap
}

urlpatterns = i18n_patterns(
   path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, 
   name='django.contrib.sitemaps.views'),
)

Now when I generate sitemap.xml I get no language prefix,

<url>
   <loc>example.com/news/data/1/</loc>
     <lastmod>2022-03-24</lastmod>
     <changefreq>daily</changefreq>
   <priority>0.5</priority>
</url>
<url>
   <loc>example.com/news/data/1/</loc>
     <lastmod>2022-01-08</lastmod>
     <changefreq>daily</changefreq>
   <priority>0.5</priority>
</url>

For the other Model without with get_absolute_url but without harcoded location - everything works fine, language prefix added correctly.

How can I fix my Sitemap code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-02-02 02:05:39

只需从您的SiteMap类中删除您的“位置”功能。由于您拥有get_absolute_url函数定义了您的模型,因此它将返回语言代码的URL。

Just remove your 'location' function from your Sitemap class. As you have get_absolute_url function defined your Model, it will return the urls oncluding the language codes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文