【问题标题】:ContentNotRenderedError after a django upgradedjango 升级后的 ContentNotRenderedError
【发布时间】:2011-10-04 23:17:32
【问题描述】:

这是我使用的中间件:

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

【问题讨论】:

  • 我不知道。但是从消息的外观来看,较新的版本不再允许您访问 process_view 中的响应内容,它应该在另一个中间件视图中访问。 =/

标签: python django middleware django-1.4


【解决方案1】:

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

【讨论】:

  • 当我升级到 django 1.4 时,这并没有解决我的应用程序中的相同问题——我仍在努力寻找解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 2013-02-17
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
  • 1970-01-01
相关资源
最近更新 更多