I was using some custom middleware to check auth of users. This was working great but I don't want to run this middleware on every url.
Suggestions point towards using @decorator_from_middleware
before each view that you want middleware to run, this would be ideal. Some of my view should be global, others behind auth.
I cant seem to import the middleware to call it in the views file.
My views.py:
from myapp.middleware import *
@decorator_from_middleware(AuthCheckMiddleware)
def index(request):
return render(request, "index.html")
My myapp.middleware.authCheck.py:
class AuthCheckMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
#CHECK AUTH HERE
response = self.get_response(request)
return response
def process_exception(self, request, exception):
return None
The error I get:
File "/vagrant/myapp/django-project/isadmin/web/views.py", line 93, in <module>
@decorator_from_middleware(AuthCheckMiddleware)
NameError: name 'AuthCheckMiddleware' is not defined