use*_*032 16 python django django-middleware django-admin
我想使用我在整个网站上编写的一些中间件(大量的页面,因此我选择不使用装饰器,因为我想使用所有页面的代码).唯一的问题是我不想将中间件用于管理代码,而且它似乎对它们有效.
有没有什么方法可以配置settings.py或urls.py,或者代码中有什么东西可以防止它在管理系统的页面上执行?
任何帮助非常感谢,
干杯
保罗
Iss*_*lly 29
一般的方法是(基于piquadrat的答案)
def process_request(self, request):
if request.path.startswith(reverse('admin:index')):
return None
# rest of method
Run Code Online (Sandbox Code Playgroud)
如果有人改变了这种方式/admin/
给/django_admin/
你还在覆盖.
您可以检查process_request中的路径(以及中间件中的任何其他进程_* - 方法)
def process_request(self, request):
if request.path.startswith('/admin/'):
return None
# rest of method
def process_response(self, request, response):
if request.path.startswith('/admin/'):
return response
# rest of method
Run Code Online (Sandbox Code Playgroud)