django 升级后出现 ContentNotRenderedError

发布于 2024-12-08 11:36:08 字数 2317 浏览 1 评论 0原文

这是我使用的一个中间件:

class StatsMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        # get number of db queries before we do anything
        n = len(connection.queries)

        # time the view
        start = time.time()
        response = view_func(request, *view_args, **view_kwargs)
        totTime = time.time() - start

        # compute the db time for the queries just run
        queries = len(connection.queries) - n
        if queries:
        dbTime = reduce(add, [float(q['time']) 
                              for q in connection.queries[n:]])
        else:
            dbTime = 0.0

        # and backout python time
        pyTime = totTime - dbTime

        stats = {
        'totTime': totTime,
        'pyTime': pyTime,
        'dbTime': dbTime,
        'queries': queries,
        'sql': '<br />'.join([ '<div class="stats_sql_query">%s</div><div class="stats_sql_time">%s s</div>' % (q['sql'], q['time']) for q in connection.queries[n:]]),
        }

        # clean query cache
        db.reset_queries()

        # replace the comment if found            
        if response and response.content:
            s = response.content
            regexp = re.compile(r'(?P<cmt><!--\s*STATS:(?P<fmt>.*?)-->)')
            match = regexp.search(s)
            if match:
                s = s[:match.start('cmt')] + \
                    match.group('fmt') % stats + \
                    s[match.end('cmt'):]
                response.content = s

        return response

它一直在 django 1.3 之前完美地工作,但是当我今天升级到 django trunk(1.4+)时,它就崩溃了,例外:-

Traceback:
File "./../django-trunk/django/core/handlers/base.py" in get_response
  105.                         response = middleware_method(request, callback, callback_args, callback_kwargs)
File "misc/middleware.py" in process_view
  63.         if response and response.content:
File "./../django-trunk/django/template/response.py" in _get_content
  123.             raise ContentNotRenderedError('The response content must be '

Exception Type: ContentNotRenderedError at /
Exception Value: The response content must be rendered before it can be accessed.

如果有人使用 django trunk 给我指出,我将不胜感激正确的方向。谢谢!

Here's a middleware that I use:

class StatsMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        # get number of db queries before we do anything
        n = len(connection.queries)

        # time the view
        start = time.time()
        response = view_func(request, *view_args, **view_kwargs)
        totTime = time.time() - start

        # compute the db time for the queries just run
        queries = len(connection.queries) - n
        if queries:
        dbTime = reduce(add, [float(q['time']) 
                              for q in connection.queries[n:]])
        else:
            dbTime = 0.0

        # and backout python time
        pyTime = totTime - dbTime

        stats = {
        'totTime': totTime,
        'pyTime': pyTime,
        'dbTime': dbTime,
        'queries': queries,
        'sql': '<br />'.join([ '<div class="stats_sql_query">%s</div><div class="stats_sql_time">%s s</div>' % (q['sql'], q['time']) for q in connection.queries[n:]]),
        }

        # clean query cache
        db.reset_queries()

        # replace the comment if found            
        if response and response.content:
            s = response.content
            regexp = re.compile(r'(?P<cmt><!--\s*STATS:(?P<fmt>.*?)-->)')
            match = regexp.search(s)
            if match:
                s = s[:match.start('cmt')] + \
                    match.group('fmt') % stats + \
                    s[match.end('cmt'):]
                response.content = s

        return response

It has been working perfectly for me up to django 1.3 but this broke when I upgraded to django trunk (1.4+) today, with the exception:-

Traceback:
File "./../django-trunk/django/core/handlers/base.py" in get_response
  105.                         response = middleware_method(request, callback, callback_args, callback_kwargs)
File "misc/middleware.py" in process_view
  63.         if response and response.content:
File "./../django-trunk/django/template/response.py" in _get_content
  123.             raise ContentNotRenderedError('The response content must be '

Exception Type: ContentNotRenderedError at /
Exception Value: The response content must be rendered before it can be accessed.

Would appreciate it if some one using django trunk points me in the right direction. Thanks!

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

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

发布评论

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

评论(1

末が日狂欢 2024-12-15 11:36:08

黑客解决方案:
您可以通过检查响应是否具有 is_rendered 属性来防止这种情况,如果有,则在更改 STATS 字符串之前它是否为 true,如下所示:

   if response:
        if (hasattr(response,'is_rendered') and response.is_rendered or not hasattr(response,'is_rendered') ) and response.content:
            s = response.content
            regexp = re.compile(r'(?P<cmt><!--\s*STATS:(?P<fmt>.*?)-->)')
            match = regexp.search(s)
            if match:
                s = s[:match.start('cmt')] + \
                    match.group('fmt') % stats + \
                    s[match.end('cmt'):]
                response.content = s

    return response

Hacktastic solution:
You can guard against this by checking if the response has an is_rendered attribute and if so, that it is true before changing the STATS string as follows:

   if response:
        if (hasattr(response,'is_rendered') and response.is_rendered or not hasattr(response,'is_rendered') ) and response.content:
            s = response.content
            regexp = re.compile(r'(?P<cmt><!--\s*STATS:(?P<fmt>.*?)-->)')
            match = regexp.search(s)
            if match:
                s = s[:match.start('cmt')] + \
                    match.group('fmt') % stats + \
                    s[match.end('cmt'):]
                response.content = s

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