Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example for enforcing login required globally using Django's internal URL patterns and resolver
# 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