/ Published in: Python

Django's middleware for global app login. With this middleware you don't have to decorate views by login_required decorator.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
from django.conf import settings from django.shortcuts import redirect from re import compile EXEMPT_URLS = [compile(settings.LOGIN_URL.lstrip('/'))] if hasattr(settings, 'LOGIN_EXEMPT_URLS'): EXEMPT_URLS += [compile(expr) for expr in settings.LOGIN_EXEMPT_URLS] class AccountsMiddleware(): def process_request(self, request): if request.user.is_anonymous(): path = request.path_info.lstrip('/') if not any(m.match(path) for m in EXEMPT_URLS): return redirect(settings.LOGIN_URL)
Comments
