【问题标题】:Middlware triggering error twice in djangodjango中的中间件两次触发错误
【发布时间】:2019-02-25 15:53:13
【问题描述】:

我为一个应用创建了一些中间件,用于检查登录用户的一些条件。

如果其中任何一个失败,它就会启动错误,让用户知道。

问题是错误在页面顶部出现了两次。

中间件如下所示:

class RegistrationMiddleware(object):

    def __init__(self, get_response):
        self.get_response = get_response
        print("In init of middleware")

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        print("Pre-view middle")
        response = self.get_response(request)
        # Code to be executed for each request/response after
        # the view is called.
        print("Post-view middle")

        ...logic stuff....

        if invalid_entries:
            for problem_reg in invalid_entries:
                messages.error(
                    request, format_html(
                        """
                        Please either
                        remove or change this registration.
                        """ 
                        )
                    )
        print('end of view reutrning response')
        return response

错误在页面上出现两次。

我的控制台显示如下:

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
In init of middleware
Pre-view middle
this is a test of the get_user method
Post-view middle
end of view reutrning response
[25/Feb/2019 09:48:46] "GET /registrations/ HTTP/1.1" 200 21860
[25/Feb/2019 09:48:46] "GET /static/styles.css HTTP/1.1" 200 7082
[25/Feb/2019 09:48:46] "GET /static/registrations/style.css HTTP/1.1" 200 2282
[25/Feb/2019 09:48:46] "GET /static/registrations/index.js HTTP/1.1" 200 1885
[25/Feb/2019 09:48:46] "GET /static/all.min.js HTTP/1.1" 200 3738182
Pre-view middle
Post-view middle
this is a test of the get_user method
end of view reutrning response
Not Found: /favicon.ico
[25/Feb/2019 09:48:47] "GET /favicon.ico HTTP/1.1" 404 2586

我不确定中间件是否 100% 是检查此类错误的最佳解决方案,但我需要检查应用程序中几乎每个视图上的相同代码位,因此这似乎是处理此问题的正确方法。

我只是想让它只触发一次 - 在视图渲染之前或之后都可以,但我只需要它显示一个错误。

    {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible mt-4" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            {{ message }}
        </div>
    {% endfor %}

【问题讨论】:

  • 从您的日志看来,您的中间件运行正常。我不确定您对一页显示两次的错误是什么意思...您如何在页面上显示错误? invalid_entries 中是否可能有多个项目?
  • 否 - 在我的开发环境中,我已经设置好了,所以我在 invalid_entries 中只显示了 1 个,但是当我刷新页面时 - 该错误消息在屏幕上显示了两次。在模板中是一个简单的{% for message in messages %} 循环
  • 您在从上面的中间件返回之前检查过messages 的值吗?
  • 否定。我目前没有针对现有消息的检查。
  • 似乎是第一次加载页面的问题:例如,我转到页面A,我看到重复错误。然后到 B 页,我看到 2 个错误。返回页面 A?我只看到一个如预期的那样。这就是为什么我试图只称它为“一次”,可以这么说。我觉得有时它会被调用两次。

标签: python django middleware django-middleware


【解决方案1】:

有逻辑之后:response = self.get_response(request) 导致问题和不一致的行为 - 因为警报正在处理 视图,进而导致错误被加载到 next 刷新和冲洗并重复,大多数时候错误是重复的。

将其移至视图处理之前解决了问题并有助于使其保持一致。

【讨论】:

    猜你喜欢
    • 2018-01-16
    • 2023-04-02
    • 2014-06-24
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2014-04-07
    相关资源
    最近更新 更多