Created
March 20, 2012 04:01
-
-
Save jteso/2131183 to your computer and use it in GitHub Desktop.
Require login middleware for Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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