
Wagtail 다국어 사이트맵
다국어 Wagtail 사이트를 위한 Google 친화적인 사이트맵
웹사이트에서 다국어 콘텐츠를 처리할 때 올바른 형식의 Sitemap 파일을 생성해야 합니다.

웹사이트에서 여러 언어로 된 콘텐츠를 생성할 때 페이지(및 기타 페이지 인덱싱 엔진)의 현지화된 버전에 대해 Google에 알려야 합니다 .
To make this work properly, you'll need your site to provide two specific files, a robots.txt, and a sitemap.xml, both at the / root of your domain. While the Wagtail CMS: Django Content Management System does have a sitemap framework that seems capable of handling i18n translations, and the Wagtail Localize is a translation plugin that makes handling translated content a breeze. However I have to say that I couldn't get properly-formatted sitemap files to generate with these tools. Language alternate tags were present, the hreflang language codes were correct, but the href tags were always pointing at the default (English) language URL.
이 문제를 해결하고 메서드를 덮어쓰는 데 많은 시간을 보낸 후에 robots.txt 에서 했던 것처럼 Django 템플릿을 사용하여 이 작업을 수행하기로 결정했습니다.
이것은 지침을 따르고 프로젝트 설정에서 언어 목록을 구성했다고 가정합니다. 예를 들면 다음과 같습니다.
# my_project/settings.py
WAGTAIL_CONTENT_LANGUAGES = LANGUAGES = [
('en', "English"),
('fr', "French"),
('es', "Spanish"),
]
I first had to create my template files, one for the robots, and one for sitemap.
Here is the content of the my_project/templates/sitemap.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
{% spaceless %}
{% for url in urlset %}
<url>
<loc>{{ url.location }}</loc>
{% if url.lastmod %}
<lastmod>{{ url.lastmod|date:'Y-m-d' }}</lastmod>
{% endif %}
{% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
{% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
{% if url.language %}<lriority>{{ url.language }}</lriority>{% endif %}
{% for item in url.alternates %}
<xhtml:link rel="alternate" hreflang="{{ item.lang_code }}" href="{{ item.location }}"/>
{% endfor %}
</url>
{% endfor %}
{% endspaceless %}
</urlset>
Here is the content of the my_project/templates/robots.txt template file:
User-Agent: *
Disallow: /admin/
# Sitemap files
Sitemap: {{ wagtail_site.root_url }}/sitemap.xml
Now the code.
Below is the content of the my_project/blog/views.py file.
It will only query active pages in the current active language. For each page, we look to see if there is a live translation in any of the other configured languages.
Make sure to set the values of changefreq and priority to whatever your needs are, the values in the script are provided as an example
Note that the value of changefreq provides search engines with general information and does not necessarily reflect the actual frequency of exploration of the page. Accepted values are:
- always
- hourly
- daily
- weekly
- monthly
- yearly
- never
The value "always" should be used to describe documents that change with each access. The value "never" should be used to describe archived URLs.
from django.views.generic import TemplateView
from wagtail.core.models import Site
from .models import BlogPage as Article
from django.urls import reverse
from django.conf import settings
from wagtail.core.models import Page
from wagtail.core.models import Locale
from django.conf import settings
class RobotsView(TemplateView):
content_type = 'text/plain'
template_name = 'robots.txt'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
request = context['view'].request
context['wagtail_site'] = Site.find_for_request(request)
return context
class SiteMap(TemplateView):
content_type = 'application/xml'
template_name = 'sitemap.xml'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
request = context['view'].request
changefreq = 'daily'
priority = 0.8
context['urlset'] = []
current = Locale.get_active().language_code
domains = settings.LANGUAGES
MySitemap = Article.objects.filter(locale=Locale.objects.get(language_code=current)).live()
for page in MySitemap:
url_info = {
'location': page.url,
'lastmod': page.last_published_at,
'changefreq': changefreq,
'priority': priority,
'alternates': [],
}
for l in domains:
lang = l[0]
if lang == current:
continue
translation = Locale.objects.get(language_code=lang)
if page.has_translation(translation):
translated_blog_page = page.get_translation(translation)
if translated_blog_page.live:
url_info['alternates'].append({
'location': translated_blog_page.url,
'lang_code': lang,
})
context['urlset'].append(url_info)
return context
마지막으로 모든 작업을 수행하고 my_project/urls.py 파일을 편집하고 다음을 추가합니다.
from my_project.blog import views as blog
...
urlpatterns = [
...
path('robots.txt', blog.RobotsView.as_view()),
path('sitemap.xml', blog.SiteMap.as_view()),
....
]
평소와 같이 두 Wagtail 구현이 동일하지 않기 때문에 블로그, BlogPage 및 Article을 정확한 구성으로 조정해야 하지만 이것이 효과가 있고 시간을 절약할 수 있기를 바랍니다.
Tagged with:
Wagtail web