Reputation: 93793
In Django: what's the best way to check whether a cookie is set before loading every page?
Background: I'm working with a site that uses LDAP auth. I want to avoid having to ask forrequest.META.get('REMOTE_USER')
on every single page, because it absolutely hammers the server: it's requested for every resource on the page, the server gets tied up, falls back to Basic auth and the user sees lots of dialogs.
Therefore, I would like to do the following on every page in the site:
REMOTE_USER
and saves it in a session-length cookie, then redirect back again.Basically, I want an approximation of the @login_required decorator, without actually using the Django login/user framework.
Any suggestions for the nicest way to do this for all pages in the site, without repeating lots of code?
many thanks!
Upvotes: 0
Views: 1485
Reputation: 1514
You want to add a middleware, see http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-request. Return a http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect if the cookie is not set, None if it's set.
Upvotes: 3