Skip to main content

Django Stats Middleware

Django is one of the most popular Python web frameworks today.

When a Django site seems slow, it's nice to put some real number to to that. The Django Debug Toolbar is the go-to middleware solution for diagnosing Django performance among other things, but it is rather large, and slow itself, adding significantly to the load time of your pages. So it's in a bit of catch-22 bind for simple performance overviews.

For that reason I wrote this tiny little lightweight piece of middleware that reports at the bottom of every page a single line reporting some basic timing stats.

  • Total Time: The time from the request arriving to the response being delivered.
  • Python Time: Time spent running Python code
  • DB Time: Time spent waiting on database queries
  • Number of Queries: The number of database queries run

Where:

​ Python Time + DB Time = Total Time

Here's an example:

sample

To use it just put it at the top of your Django Middleware stack:

MIDDLEWARE = (
    'django_stats_middleware.StatsMiddleware',
	...

That way Total Time will include the time spent in all other middleware as well as your site code.

It includes some minimal CSS and to include that in your site include it as an app (so Django knows to collect static files from it):

    INSTALLED_APPS += (
        'django_stats_middleware',
    )

and in your template load the CSS:

    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'django-stats-middleware/css/default.css' %}" />

Metadata

Release files for django-stats-middleware 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-stats-middleware 0.4
File Size Uploaded Provenance
django_stats_middleware-0.4.tar.gz 9.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-stats-middleware 0.4
File Interpreter ABI Platform
django_stats_middleware-0.4-py3-none-any.whl Python 3 none any Details

Total release size: 20.0 kB

Release files / django_stats_middleware-0.4.tar.gz

Download URL django_stats_middleware-0.4.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
929e697b472f1c6177660b746011dd96503f6dafc6e7fc4a90322abc72636478
BLAKE2b-256 checksum
How to use checksums
71ac37f7755073cdab62ad223c057b058fe17baa653822bce3963a1372cdc925
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / django_stats_middleware-0.4-py3-none-any.whl

Download URL django_stats_middleware-0.4-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f5227473fd74a179d3cbdab994cbd697e98fc83bb1407f08cad43dd8ea53cea
BLAKE2b-256 checksum
How to use checksums
cddd8031632de3ec72c918a7370676f65f70c264fc4259d9d34595951870a278
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for django-stats-middleware 0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page