Created
September 2, 2014 07:28
-
-
Save roadsideseb/af1a849c28e783aeacd7 to your computer and use it in GitHub Desktop.
Example for enforcing login required globally using Django's internal URL patterns and resolver
This file contains 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
# haiku/urls.py | |
urlpattnerns = [ | |
# all the private ones | |
] | |
public_urlpatterns = [ | |
url(....) | |
] | |
urlpattnerns += public_urlpattners | |
# middleware | |
from django.conf.urls import url, RegexURLResolver | |
from haiku.url import public_urlpatterns | |
PUBLIC_RESOLVER = RegexURLResolver(r'^/', public_urlpatterns) | |
class LoginMiddleware(object): | |
def process_request(self, request): | |
if request user.is_authenticated(): | |
return | |
try: | |
PUBLIC_RESOLVER.resolve(request.path_info) | |
except Resolver404: | |
return HttpResponseRedirect(self.login_url()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment