Skip to content

Repository files navigation

Django Never Cache

License: MIT CI PyPI Python Versions Django Versions Django Packages

This Django app provides a suite of utilities to disable caching in your sensitive views.

Requirements

  • Python >= 3.8
  • Django >= 3.2

Installation

  1. Install the package via pip:

    pip install django-never-cache
  2. Add django_never_cache to your INSTALLED_APPS in settings.py:

    INSTALLED_APPS = [
        ...
        "django_never_cache",
        ...
    ]

Usage

Mixins

NoCacheMixin

Use NoCacheMixin to disable caching for a view:

from django.views.generic import TemplateView
from django_never_cache.mixins import NoCacheMixin

class MyView(NoCacheMixin, TemplateView):
    template_name = "my_template.html"

PrivateAreaMixin

Use PrivateAreaMixin to require login and disable caching for a private area:

from django.views.generic import TemplateView
from django_never_cache.mixins import PrivateAreaMixin

class MyView(PrivateAreaMixin, TemplateView):
    template_name = "my_private_template.html"

NeverCacheMiddleware

Use NeverCacheMiddleware if you want to disable caching for the whole site:

MIDDLEWARE = [
    ...
    "django_never_cache.middlewares.NeverCacheMiddleware",
]

allow_cache

If you have added NeverCacheMiddleware to MIDDLEWARE, you can re-enable caching for a specific view using the allow_cache decorator:

from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
from django_never_cache.decorators import allow_cache

@method_decorator(allow_cache, name="dispatch")
class MyCachedView(TemplateView):
    template_name = "my_cached_template.html"

allow_cache supports both synchronous and asynchronous views.

Development

Prerequisites

  • tox
  • Golang >= 1.21

Run tests

To run the tests, run the following command:

tox

Contribute

You can contribute to this project on GitHub.

  1. Fork the repository.
  2. Create a new branch: git checkout -b my-branch-name.
  3. Install pre-commit with pre-commit install and make your changes.
  4. Commit your changes: git commit -am "Add some feature".
  5. Push your branch: git push origin my-branch-name.
  6. Create a pull request.

License

This project is released under the MIT License.

About

A lightweight Django package to simplify Cache-Control configuration for sensitive views.

Topics

Resources

Stars

9 stars

Watchers

3 watching

Forks

Releases

Used by

Contributors

Languages