Never cache Django sitemap or view url from urls.py


Import the Django never_cache decorator and use it as function wrapper instead of view decorator in the urls.py


- Go to your Django project and open the urls.py file - Import the cache decorator


from django.views.decorators.cache import never_cache


- Use it as function wrapper instead of view decorator



...
urlpatterns = [
...
url(r'^about$', app.views.about, name='about'),
url(r'^sitemap\.xml$', never_cache(sitemap), {'sitemaps': sitemaps}, name='sitemap'),
url(r'^news-sitemap\.xml$', never_cache(sitemap), { 'sitemaps': {'news': NewsViewSitemap }, 'template_name': 'news_sitemap.html' }, name='news_sitemap'),

url(r'^rss/$', never_cache(RssSiteNewsFeed()), name='rss'),
url(r'^atom/$', never_cache(AtomSiteNewsFeed()), name='atom'),
....
]


If you go in the never_cache function source code you will see


def add_never_cache_headers(response):
"""
Adds headers to a response to indicate that a page should never be cached.
"""
patch_response_headers(response, cache_timeout=-1)
patch_cache_control(response, no_cache=True, no_store=True, must_revalidate=True)


It just adds Cache-Control: max-age=0, no-cache, no-store, must-revalidate header to a response to indicate that a page should never be cached as stated in the official documentation on this link. Quick and easy! Cheers