当前位置: 首页 > 面试题库 >

Django:在每个页面上显示加载页面所需的时间

慕烨烁
2023-03-14
问题内容

在Django中,如何返回在网站的 每个* 页面中 加载页面 (而不是日期)所 花费 的时间, 不必
在每个views.py中 编写类似于以下代码的代码?
*

start = time.time()
#model operations
loadingpagetime = time.time() - start

如果使用aTEMPLATE_CONTEXT_PROCESSOR是最佳选择。
我如何从那里获得整个页面的加载时间,而不仅仅是获得模板的加载时间?

更新:

由于最初的问题似乎还不够清楚,因此这里介绍了我想做的 Python版本 的方法。

#!/usr/bin/env python
import cgitb; cgitb.enable() 
import time
print 'Content-type: text/html\n\n'

start = time.time()

print '<html>'
print '<head>'
print '</head>'
print '<body>'
print '<div>HEADER</div>'
print '<div>'
print '<p>Welcome to my Django Webpage!</p>'
print '<p>Welcome to my Django Webpage!</p>'
print '<p>Welcome to my Django Webpage!</p>'
print '</div>'

time.sleep(3)
loadingtime = time.time() - start

print '<div>It took ',loadingtime,' seconds to load the page</div>'
print '</body>'
print '</html>'

问题答案:

您可以创建一个自定义的中间件来进行记录。这是我基于http://djangosnippets.org/snippets/358/创建中间件以实现此目的的方式(我对代码进行了一些修改)。

首先,假设您的项目有一个名称:test_project,创建一个文件名middlewares.py,然后将其放置在与以下文件夹相同的文件夹中settings.py

from django.db import connection
from time import time
from operator import add
import re


class StatsMiddleware(object):

    def process_view(self, request, view_func, view_args, view_kwargs):
        '''
        In your base template, put this:
        <div id="stats">
        <!-- STATS: Total: %(total_time).2fs Python: %(python_time).2fs DB: %(db_time).2fs Queries: %(db_queries)d ENDSTATS -->
        </div>
        '''

        # Uncomment the following if you want to get stats on DEBUG=True only
        #if not settings.DEBUG:
        #    return None

        # get number of db queries before we do anything
        n = len(connection.queries)

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

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

        # and backout python time
        python_time = total_time - db_time

        stats = {
            'total_time': total_time,
            'python_time': python_time,
            'db_time': db_time,
            'db_queries': db_queries,
        }

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

        return response

其次,修改settings.py以添加您的中间件:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    # ... your existing middlewares ...

    # your custom middleware here
    'test_project.middlewares.StatsMiddleware',
)

注意:您必须像上面一样向中间件类添加完整路径,格式为:

<project_name>.<middleware_file_name>.<middleware_class_name>

第二个注意事项是我将此中间件添加到列表的末尾,因为我只想单独记录模板加载时间。如果要记录模板+所有中间件的加载时间,请把它放在MIDDLEWARE_CLASSES列表的开头(贷方为@Symmitchry)。

返回主题,下一步是修改您base.html或您想要记录加载时间的任何页面,添加以下内容:

<div id="stats">
<!-- STATS: Total: %(total_time).2fs Python: %(python_time).2fs DB: %(db_time).2fs Queries: %(db_queries)d ENDSTATS -->
</div>

注意:您可以根据<div id="stats">需要为命名,并为该div使用CSS,但不要更改comment <!-- STATS: .... -->。如果要更改它,请确保已针对created中的正则表达式模式进行了测试middlewares.py

瞧,享受统计数据

编辑:

对于经常使用CBV(基于类的视图)的用户,您可能会遇到ContentNotRenderedError上述解决方案的错误。不用担心,这是修复方法middlewares.py

    # replace the comment if found
    if response:
        try:
            # detects TemplateResponse which are not yet rendered
            if response.is_rendered:
                rendered_content = response.content
            else:
                rendered_content = response.rendered_content
        except AttributeError:  # django < 1.5
            rendered_content = response.content
        if rendered_content:
            s = rendered_content
            regexp = re.compile(
                r'(?P<cmt><!--\s*STATS:(?P<fmt>.*?)ENDSTATS\s*-->)'
            )
            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.6.x配合使用,如果您对其他版本的Django有问题,请在评论部分ping我。



 类似资料: