Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create a custom <a href="https://docs.djangoproject.com/en/dev/topics/http/middleware/">middleware</a> to log this. Here is how I create a middleware to achieve this purpose base on <a href="http://djangosnippets.org/snippets/358/">http://djangosnippets.org/snippets/358/</a> (I modified the code a bit).</p> <p>Firstly, assuming your project has a name: <code>test_project</code>, create a file name <code>middlewares.py</code>, I place it in the same folder as <code>settings.py</code>:</p> <pre><code>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: &lt;div id="stats"&gt; &lt;!-- STATS: Total: %(total_time).2fs Python: %(python_time).2fs DB: %(db_time).2fs Queries: %(db_queries)d ENDSTATS --&gt; &lt;/div&gt; ''' # 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&lt;cmt&gt;&lt;!--\s*STATS:(?P&lt;fmt&gt;.*?)ENDSTATS\s*--&gt;)') match = regexp.search(s) if match: s = (s[:match.start('cmt')] + match.group('fmt') % stats + s[match.end('cmt'):]) response.content = s return response </code></pre> <p>Secondly, modify <code>settings.py</code> to add your middleware:</p> <pre><code>MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', # ... your existing middlewares ... # your custom middleware here 'test_project.middlewares.StatsMiddleware', ) </code></pre> <p>Note: you have to add the full path to your middleware class like above, the format is:</p> <pre><code>&lt;project_name&gt;.&lt;middleware_file_name&gt;.&lt;middleware_class_name&gt; </code></pre> <p>A second note is I added this middleware to the end of the list because I just want to log the template load time alone. If you want to log the load time of templates + all middlewares, please put it in the beginning of <code>MIDDLEWARE_CLASSES</code> list (credits to @Symmitchry).</p> <p>Back to the main topic, the next step is to modify your <code>base.html</code> or whatever pages you want to log load time, add this:</p> <pre><code>&lt;div id="stats"&gt; &lt;!-- STATS: Total: %(total_time).2fs Python: %(python_time).2fs DB: %(db_time).2fs Queries: %(db_queries)d ENDSTATS --&gt; &lt;/div&gt; </code></pre> <p>Note: you can name the <code>&lt;div id="stats"&gt;</code> and use CSS for that div however you want, but DON'T change the comment <code>&lt;!-- STATS: .... --&gt;</code>. If you want to change it, be sure that you test it against the regex pattern in the created <code>middlewares.py</code>.</p> <p>Voila, enjoy the statistics.</p> <p><strong>EDIT:</strong></p> <p>For those who use CBVs (Class Based Views) a lot, you might have encountered the error <code>ContentNotRenderedError</code> with above solution. Have no fear, here is the fix in <code>middlewares.py</code>:</p> <pre><code> # 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 &lt; 1.5 rendered_content = response.content if rendered_content: s = rendered_content regexp = re.compile( r'(?P&lt;cmt&gt;&lt;!--\s*STATS:(?P&lt;fmt&gt;.*?)ENDSTATS\s*--&gt;)' ) match = regexp.search(s) if match: s = (s[:match.start('cmt')] + match.group('fmt') % stats + s[match.end('cmt'):]) response.content = s return response </code></pre> <p>I got it working with Django 1.6.x, if you have problem with other version of Django, please ping me in comment section.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload