Skip to main content

A Django middleware to trigger warnings when context keys are not used in a template.

Project description

Django Unused Context

Django Middleware providing a mechanism for triggering warnings about template context keys that do not get used while rendering the template.

Full documentation on read the docs.

Installation

  1. Install the package:
python -m pip install django-unused-context

  1. Add the middleware to your Django settings.py file:
MIDDLEWARE = [


    'django_unused_context.middleware.UnusedContextMiddleware',

    ...
]

  1. Ensure that DEBUG is set to True in your Django settings file.
DEBUG = True

Usage

Usage is automatic once the middleware is added. On each response that renders a template, any variables that were added to the template's context and then not used in the template rendering will be added to an internal set that will be both logged out and raise warnings. This is useful for knowing that all variables being sent to a template are being used.

Configuration

The package has a couple of configuration options available to you. The options can be set by adding the associated option and its value into your settings file for Django.

DJANGO_UNUSED_CONTEXT_ALWAYS

Default: False
The tool will only show warnings and log out messages when the project's settings has DEBUG = True. If you would like to have unused context variables logged out and raise warnings regardless of whether DEBUG = True, set this setting to True.

This is useful for having tests output any unused context variables. Since tests are normally run with DEBUG set to False, you will never see output from this tool when running tests. If you would like to see this output when running tests as well you can, set this setting to True.

NOTE: It is strongly Encouraged that you do NOT turn this setting on for a project that is in production as it will slow down the site and do unnecessary logging.

DJANGO_UNUSED_CONTEXT_ALWAYS = True

DJANGO_UNUSED_CONTEXT_IGNORE

Default: [] (Empty List)
The tool comes with some default keys that are automatically ignored regardless of whether they are used or not in a template. Many of these are provided by Django automatically and thus often times may not be used in a template. Rather than constantly seeing these, it seemed more reasonable to ignore them.

The predefined keys to ignore include:

'block',                  # block.super not called in overridden template.
'csrf_token',             # Given to all templates.
'DEFAULT_MESSAGE_LEVELS', # Given to all templates using messages framework.
'False',                  # Given to all templates.
'forloop',                # Given to templates with a for loop.
'is_paginated',           # Included by ListView and may not need pagination.
'None',                   # Given to all templates.
'page_obj',               # Included by ListView and may not need pagination.
'paginator',              # Included by ListView and may not need pagination.
'perms',                  # Given to login_required templates.
'root_urlconf',           # Given to exception pages for 404.
'settings',               # Likely to be given to templates and not used.
'site',                   # Given to the login page and may not be used.
'site_name',              # Given to the login page and may not be used.
'True',                   # Given to all templates.
'view',                   # Given to built-in password reset page.

If you would like to add additional keys to ignore that are specific to your project, you can do that here. Be sure to list each key in the list as a string.

DJANGO_UNUSED_CONTEXT_IGNORE = [
    'errors'
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-unused-context-0.1.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

django_unused_context-0.1.4-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file django-unused-context-0.1.4.tar.gz.

File metadata

File hashes

Hashes for django-unused-context-0.1.4.tar.gz
Algorithm Hash digest
SHA256 84ae5a9f72601f4ae75b633b1b5d383cf26b6810807881e800f9d02e0c8e9368
MD5 752e89362e713376ab0900cb3cb8b0f4
BLAKE2b-256 a4b38d0619ad040ac3a398c4f896f70a77e34ed08f679e5a8de34fa2cc00a92a

See more details on using hashes here.

File details

Details for the file django_unused_context-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_unused_context-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b59ccea8b92d9bb0a0e3b46739396291484c2b34ba7a327fc4664da0d286b1ae
MD5 b9fb9b5efe0c107028e627ecd53d1bf8
BLAKE2b-256 117408d5ba582dfa9ec8fe709cd9a223036056047c57de5f3efa2d1dafc23078

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page