1

I have the following in my views.py:

ACCEPTED = ["x", "y", "z", ...]

def index(request, param):
    if not (param in ACCEPTED):
        raise Http404
    return render(request, "index.html", {"param": param})

Url is simple enough:

path('articles/<str:param>/', views.index, name='index'),

How do I generate a sitemap for this path only for the accepted params defined in the ACCEPTED constant? Usually examples I've seen query the database for a list of detail views.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
darkhorse
  • 8,192
  • 21
  • 72
  • 148

1 Answers1

2

There are solutions in the Django documentation for your case: https://docs.djangoproject.com/en/4.1/ref/contrib/sitemaps/#sitemap-for-static-views

For your pages, do something like that:

class StaticViewSitemap(sitemaps.Sitemap):
    priority = 0.5
    changefreq = 'daily'

    def items(self):
        return ['x', 'y', 'z', ...]

    def location(self, item):
        return reverse(index, kwargs={"param": item})

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Lucas Grugru
  • 1,664
  • 1
  • 4
  • 18