python - Django 升级后出现 ContentNotRenderedError

标签 python django middleware django-1.4

这是我使用的中间件:

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 指出我正确的方向,我将不胜感激。谢谢!

最佳答案

Hacktastic 解决方案: 您可以通过检查响应是否具有 is_rendered 属性来防止这种情况发生,如果是,则在按如下方式更改 STATS 字符串之前确定它为真:

   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

关于python - Django 升级后出现 ContentNotRenderedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7655110/

相关文章:

node.js - Express.js 4 - 在静态文件之前使用中间件进行身份验证

python - 在 Python 3.4 中使用 for 循环从数组中删除特定元素

python - pandas .loc 返回不一致的类型

django - 将 django 与旧版身份验证 cookie 一起使用

python - Django自定义中间件渲染没有csrf token

python - 在 django 中从中间件生成/隐藏消息

python - 使用 os.walk 制作 root 列表

python - 使用 numpy 'module' 对象没有属性 'array'

python - 如何在没有 Django 其余部分的情况下使用 Django 模板?

python - Django 与巨大的 mysql 数据库