To "mark" a url or a group of urls as requiring authorization, you can use the decorator:
urls.py
from django.contrib.auth.decorators import login_required urlpatterns = [ ... url(r"^select2/fields/auto.json$", login_required(AutoResponseView.as_view()), name="django_select2-json"), ... ]
If you want to close the entire site from unauthorized users, then you should use middleware. For example, as follows:
middleware.py
from django.http import HttpResponseRedirect from django.conf import settings 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 LoginRequiredMiddleware: if not request.user.is_authenticated(): path = request.path_info.lstrip('/') if not any(m.match(path) for m in EXEMPT_URLS): return HttpResponseRedirect(settings.LOGIN_URL)
It must be registered in MIDDLEWARE_CLASSES:
settings.py
MIDDLEWARE_CLASSES = [ ... appname.middleware.LoginRequiredMiddleware, ... ]