Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do I integrate a 304 in Django?

When a user requests the same page, with the same data...I'd like Django to return a 304, so that the browser doesn't have to load the page all over again.

I'm new to this. How can this be done?

Thanks.

like image 517
TIMEX Avatar asked Feb 18 '10 09:02

TIMEX


2 Answers

There's extensive description in Django documentation: Conditional view processing

Following tools are particularly useful:

  1. @last_modified and @etag view decorators. You supply them with a function to compute the value from request and everything else is done automatically.
  2. django.middleware.http.ConditionalGetMiddleware -- it generates required ETag and returns 304 if there's a cache hit, but this still takes server time to generate full HTML and only network time is saved. Still very good for one-line configuration change.
like image 150
Alexander Lebedev Avatar answered Sep 22 '22 05:09

Alexander Lebedev


You could look into Django's caching system, or if you can easily check if the user is requesting the same data, you can return a HttpResponseNotModified() - this returns a 304. Check out the docs here.

like image 23
hora Avatar answered Sep 18 '22 05:09

hora