Example of using django localeurl with sitemaps. Create sitemap instance for each combination of the sitemap section and language.
In your sitemap class create method
def location(self, obj):
return chlocale(obj.get_absolute_url(), self.language)
or inherit it from LocaleurlSitemap class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # example Sitemap
class AdvertisementsSitemap(Sitemap):
def __init__(self, language):
self.language = language
def items(self):
return Advertisement.active_objects.all()
def location(self, obj):
# change locale in the url for the language for this sitemap
return chlocale(obj.get_absolute_url(), self.language)
----------------------------------------
# create each section in all languages
sitemaps = {
'advertisements-sk': sitemaps.AdvertisementsSitemap('sk'),
'advertisements-cs': sitemaps.AdvertisementsSitemap('cs'),
}
# add sitemap into urls
urlpatterns = patterns('',
url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}),
url(r'^sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
)
-------------------------------
# language dependent sitemap class
class LocaleurlSitemap(Sitemap):
def __init__(self, language):
self.language = language
def location(self, obj):
return chlocale(obj.get_absolute_url(), self.language)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
very helpful! if you have a lot of languages, you can do something like this:
sitemaps = {}
for language in settings.LANGUAGES:
#
Please login first before commenting.