Advertisement
kastielspb

Django sitemap

Jul 30th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. # settings.py
  2. INSTALLED_APPS = [
  3.     ...
  4.     'django.contrib.sitemaps',
  5.     ...
  6. ]
  7. ...
  8.  
  9. ROBOTS_USE_SITEMAP = True
  10. ROBOTS_SITEMAP_VIEW_NAME = 'main_sitemap'
  11.  
  12. # app/urls.py
  13. ...
  14. from django.contrib.sitemaps import views as sitemap_views
  15.  
  16. from some_app.sitemap import (
  17.     CategoriesSitemap,
  18.     ProductSitemap
  19. )
  20.  
  21.  
  22. sitemaps = {
  23.     'categories': CategoriesSitemap,
  24.     'products': ProductSitemap,
  25. }
  26.  
  27. urlpatterns = [
  28.     url(
  29.         r'^sitemap\.xml$',
  30.         sitemap_views.index,
  31.         {'sitemaps': sitemaps},
  32.         name='main_sitemap'
  33.     ),
  34.     url(
  35.         r'^sitemap-(?P<section>.+)\.xml$',
  36.         sitemap_views.sitemap,
  37.         {'sitemaps': sitemaps},
  38.         name='django.contrib.sitemaps.views.sitemap'
  39.     ),
  40.     ...
  41. ]
  42.  
  43. # some_app.sitemap.py
  44. from django.contrib.sitemaps import Sitemap
  45.  
  46. from .models import Product, Categories
  47.  
  48. __all__ = (
  49.     'ProductSitemap',
  50.     'CategoriesSitemap'
  51. )
  52.  
  53.  
  54. class CategoriesSitemap(Sitemap):
  55.     changefreq = "monthly"
  56.     priority = 0.9
  57.     protocol = "https"
  58.  
  59.     def items(self):
  60.         return Categories.objects.exclude(depth=2)
  61.        
  62.  
  63. class ProductSitemap(Sitemap):
  64.     changefreq = "monthly"
  65.     priority = 0.9
  66.     protocol = "https"
  67.  
  68.     def items(self):
  69.         return Product.objects.filter(variants__amount__gte=1)
  70.  
  71.     def lastmod(self, obj):
  72.         return obj.updated_at
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement