多语言sitemap,get_absolute_url和位置
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需从您的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.