Cache-Control header for Flat Pages

31 views
Skip to first unread message

Web Architect

unread,
Sep 29, 2016, 12:30:34 AM9/29/16
to Django users
Hi,

We are using Django Flat pages for some static pages like About us, privacy policy etc. We are using Web Accelerator (like Varnish) in front of Django. Hence, would like to set the Cache-Control Header to cache the flat pages. I am not able to figure how to do that for flat pages.

Tried using the middleware UpdateCacheMiddleware but it's not working. Would really appreciate if someone could throw some light.

Thanks.

Sergiy Khohlov

unread,
Sep 29, 2016, 3:35:02 AM9/29/16
to django-users
could you please example your view.py   of those  flat pages. In case of CBV syntax is little different  but it is possible to add any  response header using view. 

Many thanks,

Serge


+380 636150445
skype: skhohlov

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/38b38234-21bf-487a-9497-8120f7699513%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Web Architect

unread,
Sep 29, 2016, 6:47:12 AM9/29/16
to Django users
Hi Serge,

Thanks for your response.

We do not have any Views implemented for flatpages. I think they are Django internal stuff for static html content (something like a CMS):


Have used the url pattern as in the example mentioned in the link above:

from django.contrib.flatpages import views

urlpatterns += [
    url(r'^about-us/$', views.flatpage, {'url': '/about-us/'}, name='about'),
    url(r'^license/$', views.flatpage, {'url': '/license/'}, name='license'),
]

Thanks.

On Thursday, September 29, 2016 at 1:05:02 PM UTC+5:30, Sergiy Khohlov wrote:
could you please example your view.py   of those  flat pages. In case of CBV syntax is little different  but it is possible to add any  response header using view. 

Many thanks,

Serge


+380 636150445
skype: skhohlov

On Thu, Sep 29, 2016 at 7:30 AM, Web Architect <pina...@gmail.com> wrote:
Hi,

We are using Django Flat pages for some static pages like About us, privacy policy etc. We are using Web Accelerator (like Varnish) in front of Django. Hence, would like to set the Cache-Control Header to cache the flat pages. I am not able to figure how to do that for flat pages.

Tried using the middleware UpdateCacheMiddleware but it's not working. Would really appreciate if someone could throw some light.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Michal Petrucha

unread,
Sep 29, 2016, 7:38:08 AM9/29/16
to Django users
On Thu, Sep 29, 2016 at 03:47:11AM -0700, Web Architect wrote:
> Hi Serge,
>
> Thanks for your response.
>
> We do not have any Views implemented for flatpages. I think they are Django
> internal stuff for static html content (something like a CMS):
>
> https://docs.djangoproject.com/en/1.10/ref/contrib/flatpages/
>
> Have used the url pattern as in the example mentioned in the link above:
>
> from django.contrib.flatpages import views
> urlpatterns += [
> url(r'^about-us/$', views.flatpage, {'url': '/about-us/'}, name='about'),
> url(r'^license/$', views.flatpage, {'url': '/license/'}, name='license'),]

You can use the cache_control view decorator to wrap the flatpage view
before you plug it into your urlpatterns::

from django.contrib.flatpages import views
from django.views.decorators.cache import cache_control

cached_flatpage = cache_control(max_age=4700)(views.flatpage)

urlpatterns += [
url(r'^about-us/$', cached_flatpage, {'url': '/about-us/'}, name='about'),
url(r'^license/$', cached_flatpage, {'url': '/license/'}, name='license'),]

Cheers,

Michal
signature.asc

Sergiy Khohlov

unread,
Sep 29, 2016, 8:14:43 AM9/29/16
to django-users
Another way  is writing middleware class such as: 

 class MyFlatMiddleware(FlatpageFallbackMiddleware):
   def process_response(self, request, response):
        response =super(sMyFlatMiddleware, self).process_response(request, response)
       response['HEADER_NAME'] = "HEADER_VALUE"
       return response  

Many thanks,

Serge


+380 636150445
skype: skhohlov

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Web Architect

unread,
Sep 29, 2016, 8:17:55 AM9/29/16
to Django users
Hi Michal,

Thanks for your response. My mistake that I should have mentioned that we are using Django 1.8. The decorator cache_control I think was introduced in 1.9. Would there be something similar in 1.8?

Thanks.

Michal Petrucha

unread,
Sep 29, 2016, 9:15:35 AM9/29/16
to Django users
On Thu, Sep 29, 2016 at 05:17:54AM -0700, Web Architect wrote:
> Hi Michal,
>
> Thanks for your response. My mistake that I should have mentioned that we
> are using Django 1.8. The decorator cache_control I think was introduced in
> 1.9. Would there be something similar in 1.8?

It's also in 1.8:
https://docs.djangoproject.com/en/1.8/topics/cache/#controlling-cache-using-other-headers

(And yeah, I just verified that I can import it in a 1.8 project.)

Michal
signature.asc
Reply all
Reply to author
Forward
0 new messages