Login

Localeurl sitemap

Author:
tomas
Posted:
July 10, 2009
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

matomick (on January 14, 2010):

very helpful! if you have a lot of languages, you can do something like this:

sitemaps = {}

for language in settings.LANGUAGES:

sitemaps['advertisements-%s' %language[0]] = AdvertisementsSitemap(language[0])

#

Please login first before commenting.