And if you have several Sitemaps classes, you can use the mixin approach.
Example for Django 1.5.1.
from django.contrib.sitemaps import Sitemap
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from yourapp.models import MyObj
class SiteMapDomainMixin(Sitemap):
def get_urls(self, page=1, site=None, protocol=None):
fake_site = Site(domain='mydomain.com', name='mydomain.com')
return super(SiteMapDomainMixin, self).get_urls(page, fake_site, protocol=None)
class MySitemap(SiteMapDomainMixin):
changefreq = "never"
priority = 0.5
def items(self):
return MyObj.objects.all().order_by('pk')[:1000]
def location(self, item):
return reverse('url_for_access_myobj', args=(item.slug,))
def lastmod(self, obj):
return obj.updated_at
class AnotherSitemap(Sitemap):
changefreq = "never"
priority = 0.5
def items(self):
return ['url_1', 'url_2', 'url_3',]
def location(self, item):
return reverse(item)
Urls.py url will look like ...
from sitemaps import MySitemap
from sitemaps import AnotherSitemap
from yourapp.views import SomeDetailMyObjView
admin.autodiscover()
sitemaps = {
'mysitemap': MySitemap,
'anothersitemap': AnotherSitemap,
}
urlpatterns = patterns('',
url(r'^accessing-myobj/(?P<myobj_slug>[-\w]+)$', SomeDetailMyObjView, name='url_for_access_myobj'),
(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
)
source
share