Package: django-qartez
The missing XML sitemaps for Django.FeaturesAt the moment the following XML sitemaps are implemented:qartez.sitemaps.ImagesSitemap: XML images sitemaps according to thespecs.qartez.sitemaps.StaticSitemap: Sitemap for service pages. Add named
patterns or URLs to the sitemap to have it nicely displayed in a separate
service XML sitemap.qartez.sitemaps.RelAlternateHreflangSitemap: Sitemaps: rel=”alternate”
hreflang=”x” implementation. Read thespecs.PrerequisitesDjango: 1.11, 2.0, 2.1, 2.2 and 3.0.Python: 2.7, 3.5, 3.6, 3.7, 3.8Installation1. InstallLatest stable version on PyPI:pipinstalldjango-qartezLatest stable version from GitHub:pipinstallhttps://github.com/barseghyanartur/django-qartez/archive/stable.tar.gz2. Addqartezto yourINSTALLED_APPSINSTALLED_APPS=(# ...'django.contrib.sitemaps','qartez',# ...)Usage and examplesWe have an imaginary foo app.See theexample code.foo/sitemap.pyfromdjango.contrib.sitemapsimportSitemapfromqartez.sitemapsimport(ImagesSitemap,StaticSitemap,RelAlternateHreflangSitemap)fromfoo.modelsimportFooItem# ---------------------- XML images sitemap part ---------------------------# Dictionary to feed to the images sitemap.foo_item_images_info_dict={# Base queryset to iterate when procuding a site map'queryset':FooItem._default_manager.exclude(image=None),'image_location_field':'image_url',# Image location (URL)'image_title_field':'title',# Image title# An absolute URL of the page where image is shown'location_field':'get_absolute_url'}# XML images sitemap.foo_item_images_sitemap={'foo_item_images':ImagesSitemap(foo_item_images_info_dict,priority=0.6),}# ---------------------- Static sitemap part ---------------------------# Sitemap for service pages like welcome and feedback.foo_static_sitemap=StaticSitemap(priority=0.1,changefreq='never')foo_static_sitemap.add_named_pattern('foo.welcome')foo_static_sitemap.add_named_pattern('foo.contact')# ---------------------- Normal sitemap part ---------------------------# Normal Foo items sitemap.classFooItemSitemap(Sitemap):changefreq="weekly"priority=1.0deflocation(self,obj):returnobj.get_absolute_url()deflastmod(self,obj):returnobj.date_publisheddefitems(self):returnFooItem._default_manager.all()# ---------------------- Alternate hreflang sitemap part ---------------# Alternate hreflang sitemap.classArticleSitemap(RelAlternateHreflangSitemap):# If you want to serve the links on HTTPS.protocol='https'defalternate_hreflangs(self,obj):return[('en-us',obj.alternative_object_url),]defitems(self):returnFooItem._default_manager.all()urls.pyfromfoo.sitemapimportfoo_item_images_sitemap,foo_static_sitemapfromfoo.sitemapimportFooItemAlternateHreflangSitemap,FooItemSitemapsitemaps={'foo-items':FooItemSitemap,'foo-items-alternate-hreflang':FooItemAlternateHreflangSitemap,'foo-static':foo_static_sitemap}urlpatterns=[# Sitemaps(r'^sitemap\.xml$','django.contrib.sitemaps.views.index',{'sitemaps':sitemaps},),(r'^sitemap-foo-images\.xml$','qartez.views.render_images_sitemap',{'sitemaps':foo_item_images_sitemap},),]Note, that it’s necessary to add the`'template_name': 'qartez/rel_alternate_hreflang_sitemap.xml'`only in case if you are going to use theqartez.RelAlternateHreflangSitemap.(r'^sitemap-(?P<section>.+)\.xml$','django.contrib.sitemaps.views.sitemap',{'sitemaps':sitemaps,'template_name':'qartez/rel_alternate_hreflang_sitemap.xml'}),In order to just get a better idea what kind of models and views are given in
the example, see the code parts below.foo/models.pyclassFooItem(models.Model):title=models.CharField(_("Title"),max_length=100)slug=models.SlugField(_("Slug"),unique=True)body=models.TextField(_("Body"))date_published=models.DateTimeField(_("Date published"),blank=True,null=True,auto_now_add=True)# Image to be used for XML images sitemap.image=models.ImageField(_("Headline image"),blank=True,null=True,upload_to='foo-images')# URL to be used for alternative hreflang attribute.alternative_url=models.URLField(_("Alternative URL"),blank=True,null=True)classMeta:verbose_name=_("Foo item")verbose_name_plural=_("Foo items")def__str__(self):returnself.titledefget_absolute_url(self):kwargs={'slug':self.slug}returnreverse('foo.detail',kwargs=kwargs)# Shortcut to full image URL for XML images sitemap.defimage_url(self):returnself.image.urlifself.imageelse''foo/views.py# Service welcome pagedefwelcome(request,template_name='foo/welcome.html'):context={}returnrender_to_response(template_name,context,context_instance=RequestContext(request))# Service contact pagedefcontact(request,template_name='foo/contact.html'):context={}returnrender_to_response(template_name,context,\context_instance=RequestContext(request))foo/urls.pyurlpatterns=patterns('foo.views',# ...# Contact URLurl(r'^contact/$',view='contact',name='foo.contact'),# ...# Welcome URLurl(r'^welcome/$',view='welcome',name='foo.welcome'),# ...)LicenseGPL-2.0-only OR LGPL-2.1-or-laterSupportFor any issues contact me at the e-mail given in theAuthorsection.AuthorArtur Barseghyan <artur.barseghyan@gmail.com>