Skip to content

Instantly share code, notes, and snippets.

@jteso
Created March 20, 2012 04:01
Show Gist options
  • Save jteso/2131183 to your computer and use it in GitHub Desktop.
Save jteso/2131183 to your computer and use it in GitHub Desktop.
Require login middleware for Django
from django.conf import settings
from django.http import HttpResponseRedirect
import re
class RequireLoginMiddleware(object):
def __init__(self):
self.urls = tuple([re.compile(url) for url in settings.LOGIN_REQUIRED_URLS])
self.require_login_path = getattr(settings, 'LOGIN_URL', '/accounts/login/')
def process_request(self, request):
for url in self.urls:
if url.match(request.path) and request.user.is_anonymous():
return HttpResponseRedirect('%s?next=%s' % (self.require_login_path, request.path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment