Django中的非全局中间件 [英] Non-global middleware in Django

查看:112
本文介绍了Django中的非全局中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中有一个设置文件来定义要在每个请求上运行的中间件。这个中间件设置是全局的。有没有办法在每个视图的基础上指定一组中间件?我想要具体的URL使用一组不同于全局集的中间件。

解决方案

你想要 decorator_from_middleware

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $¬$ b b b b b b b b b b b b b b b b b b b b b def view_function(request):
#blah blah

它不适用于URL,但它的作用是每次观看,所以您可以对其效果进行细粒度的控制。


In Django there is a settings file that defines the middleware to be run on each request. This middleware setting is global. Is there a way to specify a set of middleware on a per-view basis? I want to have specific urls use a set of middleware different from the global set.

解决方案

You want decorator_from_middleware.

from django.utils.decorators import decorator_from_middleware

@decorator_from_middleware(MyMiddleware)
def view_function(request):
    #blah blah

It doesn't apply to URLs, but it works per-view, so you can have fine-grained control over its effect.

这篇关于Django中的非全局中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆