Django 中的非全局中间件
在 Django 中,有一个设置文件定义要在每个请求上运行的中间件。该中间件设置是全局的。有没有办法在每个视图的基础上指定一组中间件?我想让特定的 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您想要
decorator_from_middleware
。它不适用于 URL,但它适用于每个视图,因此您可以对其效果进行细粒度控制。
You want
decorator_from_middleware
.It doesn't apply to URLs, but it works per-view, so you can have fine-grained control over its effect.
对于这个问题我有一个真正的解决方案。警告;这有点像黑客攻击。
编辑:删除了运行视图两次的先前版本。
I have a real solution for this issue. Warning; it's a little bit of a hack.
Edit: removed previous version that ran the view twice.
这是我最近用来解决您在对 Ned 的答案的评论中提出的场景的解决方案...
它假设:
A)这是一个自定义中间件,或者您可以使用自己的中间件类扩展/包装的中间件
B)您的逻辑可以等到
process_view
而不是process_request
,因为在process_view
中,您可以在解析后检查view_func
参数。 (或者您可以调整下面的代码以使用urlresolvers
,如 Ignacio 所示)。可能有一种方法可以进一步概括这种模式,但这很好地实现了我的目标。
为了回答您更普遍的问题,我认为 Django 库中目前没有任何内容可以帮助您解决此问题。如果 django-users 邮件列表还没有解决这个问题,那么这将是一个很好的主题。
Here's a solution I used recently to address the scenario you presented in a comment to Ned's answer...
It assumes that:
A) this is a custom middleware or one that you can extend/wrap with your own middleware class
B) your logic can wait until
process_view
instead ofprocess_request
, because inprocess_view
you can inspect theview_func
parameter after it's been resolved. (Or you can adjust the code below to useurlresolvers
as indicated by Ignacio).There may be a way to generalize this pattern further, but this accomplished my goal fairly well.
To answer your more general question, I don't think there is anything in the Django libraries to help you out with this currently. Would be a good topic for the django-users mailing list if it hasn't already been addressed there.
您可以使用 process_view 方法,该方法在调用视图函数之前调用。在 process_view 中,您可以检查 — 该视图是否需要此中间件拦截。
You can use process_view method, that is called before calling the view func. In process_view you can check — if this view requires this middleware interception.
我能找到的最好的事情是使用 if request.path_info.startswith('...') 通过返回请求来跳过中间件。现在,您可以为了跳过而创建中间件,然后继承它。也许你可以做一些更简单的事情,并将该列表保存在你的settings.py中,然后跳过所有这些。如果我有任何错误,请告诉我。
(注:据我记得,我此时使用的是 Django 1.2)
The best thing I've been able to find is using if request.path_info.startswith('...') to skip over the middleware by just returning the request. Now, you could create middleware just for the sake of skipping and then inherit that. Maybe you could do something even simpler and save that list in your settings.py and then skip all those. If I'm wrong in any way, let me know.
(Note: from what I remember, I was using Django 1.2 at this time)
使用
django.core.urlresolvers.resolve()
与中间件包装器中的
request.path
相对应,以尝试查看视图是否在应用程序内,如果是,则跳过处理。Use
django.core.urlresolvers.resolve()
againstrequest.path
in a wrapper for the middleware to try to see if the view is within the app, and skip processing if so.我认为这是从中间件中排除视图的简单方法
I think this is the easy way to exclude a view from middleware
Django urlmiddleware 允许将中间件仅应用于映射到特定 url 的视图。
Django urlmiddleware allows to apply middleware only to views that are mapped to specific urls.