Django 中间件:如何从中间件访问视图的参数

发布于 2024-08-20 08:30:00 字数 378 浏览 4 评论 0原文

假设我有一个视图:

def pony_view(request, arg1, arg2):
    ... Make ponies, etc ...

和一个中间件:

class MyMiddleware(object):
    def process_request(request):
        # How do I access arg1, arg2 here?

当然 arg1 和 arg2 将通过 URL 参数和 urls.py 传入。

我需要这样做的原因是因为我想在视图函数运行之前向 request.session 添加一些内容(不过我需要从 URL 中获取一些内容)。

Let's say I have a view:

def pony_view(request, arg1, arg2):
    ... Make ponies, etc ...

And a middleware:

class MyMiddleware(object):
    def process_request(request):
        # How do I access arg1, arg2 here?

Of course arg1, and arg2 will be passed in via URL params with urls.py.

The reason I need to do this is because I want to add something to request.session before the view function runs (something that I need from the URL though).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮世清欢 2024-08-27 08:30:00

您必须实施 process_view 方法。

它具有以下签名:

process_view(self, request, view_func, view_args, view_kwargs)

并在调用视图函数之前执行:

process_view() 在 Django 调用视图之前被调用。它应该返回 NoneHttpResponse 对象。如果它返回 None,Django 将继续处理此请求,执行任何其他 process_view() 中间件,然后执行相应的视图。如果它返回一个 HttpResponse 对象,Django 将不会调用任何其他请求、视图或异常中间件或适当的视图;它会返回 HttpResponse。每次响应时都会调用响应中间件。

然后您应该能够通过以下方式访问 arg1arg2

class MyMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        arg1, arg2 = view_args[:2]

You will have to implement the process_view method.

It has this signature:

process_view(self, request, view_func, view_args, view_kwargs)

and is executed before the view function is called:

process_view() is called just before Django calls the view. It should return either None or an HttpResponse object. If it returns None, Django will continue processing this request, executing any other process_view() middleware and, then, the appropriate view. If it returns an HttpResponse object, Django won't bother calling ANY other request, view or exception middleware, or the appropriate view; it'll return that HttpResponse. Response middleware is always called on every response.

Then you should be able to access arg1 and arg2 with:

class MyMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        arg1, arg2 = view_args[:2]
作妖 2024-08-27 08:30:00

如果你想从 URL 读取 GET 参数,你可以使用 request.GET 字典:

class MyMiddleware(MiddlewareMixin):
    def process_request(self, request):
        print(request.GET)  # immutable QueryDict

If you want to read GET parameters from URL you can use request.GET dictionary:

class MyMiddleware(MiddlewareMixin):
    def process_request(self, request):
        print(request.GET)  # immutable QueryDict
独自唱情﹋歌 2024-08-27 08:30:00

您可以使用 resolve() 访问中间件的 __call__process_requestprocess_response 方法中的 URL 参数,如下所示:

from django.urls import resolve

class MyMiddleware:
  def __init__(self, get_response):
    self.get_response = get_response

  def __call__(self, request):
    match = resolve(request.path_info)
    arg1, arg2 = match.args[:2]

    # do something with arg1, arg2

    return self.get_response(request)

You can access URL parameters in the middleware's __call__, process_request or process_response methods by using resolve() like this:

from django.urls import resolve

class MyMiddleware:
  def __init__(self, get_response):
    self.get_response = get_response

  def __call__(self, request):
    match = resolve(request.path_info)
    arg1, arg2 = match.args[:2]

    # do something with arg1, arg2

    return self.get_response(request)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文