我需要在执行某些视图期间通过令牌检查身份验证,而某些视图可以在不进行身份验证的情况下访问。 那么,我如何制作中间件并从中排除一些视图。 任何其他想法来解决这个问题是值得赞赏的。
答案 0 :(得分:0)
我建议从Django提供的csrf middleware中获取灵感
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE localhost
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1350:19)
at listenInCluster (net.js:1408:12)
at Server.listen (net.js:1503:5)
at Object.<anonymous> (/media/shikhar/D/Study/git-test/node-express/index.js:30:8)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
创建一个装饰器来包装您的视图
from django.utils.deprecation import MiddlewareMixin
class MyAuthenticationMiddleware(MiddlewareMixin):
def process_view(self, request, callback, callback_args, callback_kwargs):
if getattr(callback, 'my_exempt_flag', False):
return None
# Authentication goes here
# Return None if authentication was successful
# Return a HttpResponse with some error status if not successful
可以这样使用
from functools import wraps
def exempt_from_my_authentication_middleware(view_func):
def wrapped_view(*args, **kwargs):
return view_func(*args, **kwargs)
wrapped_view.my_exempt_flag = True
return wraps(view_func)(wrapped_view)