Localeurl sitemap

 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. convenience parent class for UserProfile model by willhardy 4 years, 11 months ago
  2. update_or_create by abhin4v 5 years, 6 months ago
  3. Class based generic views that automatically check permissions by humphreymurray 3 years, 3 months ago
  4. Template Tag Render Decorator by stephen_mcd 4 years, 1 month ago
  5. Binding pre-existing tables with dynamically created class models by rodsenra 6 years, 9 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])

#

(Forgotten your password?)