Small Django utilities built on Django core.
Project description
dj-corekit
Small Django utilities built on Django core.
Installation
python -m pip install dj-corekit
Toolkit
Cache
- Cache function views with Django-style default keys or caller-owned cache keys.
- Cache class-based views with
method_decoratororCachePageMixin. - Apply headers before storing responses.
- Set Cloudflare edge cache headers separately from browser cache headers.
- Cache
TemplateResponseobjects after rendering. - Bypass caching per request with
request.do_not_cacheoronly_if.
More utilities can be added here as new modules land.
Cache
Function Views
from dj_corekit.cache import cache_page
@cache_page(
timeout=300,
headers=lambda request, response: {
"X-Cache-Scope": "product",
"Cache-Control": "public, max-age=300",
},
)
def product_detail(request):
...
Cloudflare Edge Cache
cloudflare_cache_page sets origin response headers for Cloudflare, but it does
not configure your Cloudflare zone. For HTML or other dynamic pages, Cloudflare
requires a Cache Rule that makes the route eligible for cache.
from dj_corekit.cache import cloudflare_cache_page
@cloudflare_cache_page(
timeout=300,
key_func=lambda request: f"product:{request.GET.get('id')}",
cloudflare={
"browser_ttl": 0,
"vary": ("CF-IPCountry", "CF-Device-Type"),
},
bypass_cache={
"mode": "skip",
"cookies": True,
"headers": ("Authorization",),
},
)
def product_detail(request):
...
Cloudflare behavior used by this decorator:
Cloudflare-CDN-Cache-Control: max-age=Ncontrols Cloudflare edge TTL without forwarding that header downstream. Cloudflare treats this as the Cloudflare-specific version ofCDN-Cache-Control.Cache-Control: public, max-age=Ncontrols browser-facing freshness. The default browser TTL is0, so browsers must revalidate while Cloudflare can still keep an edge copy.cloudflare["vary"]does two things: it adds the listed request headers to this package's Django cache key, and it emitsVaryso Cloudflare can cache separate variants when the zone has matching Cache Rules Vary/custom cache key configuration.- Dynamic content such as HTML is not cached by Cloudflare by default. Use a Cache Rule for routes that should be cached at the edge.
Cloudflare references:
Cloudflare-CDN-Cache-ControlVaryand Cache Rules Vary- Cache Rules settings
- Default cache behavior
- Get started with Cache
Class-Based Views
Use Django's method_decorator when you want to keep caching outside the view
class:
from django.utils.decorators import method_decorator
from django.views import View
from dj_corekit.cache import cache_page
@method_decorator(
cache_page(timeout=300, key_func=lambda request: "product:list"),
name="dispatch",
)
class ProductListView(View):
...
Use CachePageMixin when each view owns its cache settings:
from django.views import View
from dj_corekit.cache import CachePageMixin
class ProductDetailView(CachePageMixin, View):
cache_timeout = 300
cache_headers = {"X-Cache-Scope": "product"}
def get_cache_key(self, request):
return f"product:{self.kwargs['pk']}"
Use CloudflareCachePageMixin for the same class-based pattern with
Cloudflare-aware headers:
from django.views import View
from dj_corekit.cache import CloudflareCachePageMixin
class ProductDetailView(CloudflareCachePageMixin, View):
cache_timeout = 300
cache_cloudflare = {"vary": ("CF-IPCountry",)}
Behavior
Without extension params, cache_page delegates to Django's
django.views.decorators.cache.cache_page, using cache_alias as Django's
cache argument. With headers, only_if, or bypass_cache but no
key_func, Django still owns cache keying and storage; dj-corekit only wraps
the view to apply headers or bypass caching. With key_func, the callable
returns the full cache key and dj-corekit stores the response through
django.core.cache.caches[cache_alias].
POST responses are only cached when you provide key_func; include the request
method, body/form data, user, or any other response-changing input in that key.
Only 200 responses are cached. TemplateResponse objects are cached after
rendering. Set request.do_not_cache = True or return False from only_if
to bypass caching.
Request headers and cookies are not special by default. Set bypass_cache with
mode="skip" to skip cache when the request has any listed header or any
cookie.
API
cache_page
cache_page(
timeout,
key_func=None,
*,
headers=None,
cache_alias="default",
only_if=None,
bypass_cache=None,
)
cloudflare_cache_page
cloudflare_cache_page(
timeout,
key_func=None,
*,
cloudflare=None,
headers=None,
cache_alias="default",
only_if=None,
bypass_cache=None,
)
CachePageMixin
class CachePageMixin:
cache_timeout = 300
cache_key_func = None
cache_headers = None
cache_alias = "default"
cache_only_if = None
cache_bypass = None
def get_cache_key(self, request):
...
CloudflareCachePageMixin
class CloudflareCachePageMixin(CachePageMixin):
cache_cloudflare = None
timeout: seconds, or a callable receiving the response.key_func: optional callable receiving the request and returning the full cache key.headers: dict or callable receiving(request, response)and returning a header dict. These headers are applied before the response is cached.cloudflare: optional dict forcloudflare_cache_page:edge_ttl:Cloudflare-CDN-Cache-Controlmax-age. Defaults totimeout.browser_ttl: browserCache-Controlmax-age. Defaults to0.vary: request header names to include in the cache key andVary.
cache_cloudflare: mixin equivalent ofcloudflare.- Cloudflare TTL values must resolve to non-negative integers.
cache_alias: Django cache alias, default"default".only_if: callable receiving the request. ReturnFalseto bypass cache.bypass_cache: optional dict.{"mode": "skip", "cookies": True, "headers": ("Authorization",)}skips cache when the request has any cookie or any listed header.
Development
Local Checks
python -m pip install -e ".[dev]"
python -m pytest
ruff check .
ruff format --check .
tox
CI
GitHub Actions runs tests on Python 3.10, 3.11, 3.12, and 3.13. Ruff linting and formatting checks run once on Python 3.12.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dj_corekit-0.3.0.tar.gz.
File metadata
- Download URL: dj_corekit-0.3.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a7d3bf83a55e58d58e1c4b43a094b4c6d97114547192ba0a53efad0997cee64
|
|
| MD5 |
27de9270f927c5b05efff5f9621dc81f
|
|
| BLAKE2b-256 |
932977260b1372544af6c25af4bc48c9b34b0b53c322d6cc505a951c29efa711
|
Provenance
The following attestation bundles were made for dj_corekit-0.3.0.tar.gz:
Publisher:
release.yml on himkit/dj-corekit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_corekit-0.3.0.tar.gz -
Subject digest:
3a7d3bf83a55e58d58e1c4b43a094b4c6d97114547192ba0a53efad0997cee64 - Sigstore transparency entry: 2114895274
- Sigstore integration time:
-
Permalink:
himkit/dj-corekit@79e4c040839db01145777b30cb7b18cea5c286ba -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/himkit
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79e4c040839db01145777b30cb7b18cea5c286ba -
Trigger Event:
release
-
Statement type:
File details
Details for the file dj_corekit-0.3.0-py3-none-any.whl.
File metadata
- Download URL: dj_corekit-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42bf52d02da7575f68b6d1c23c2d0a415b326122220dd3ca8756da4177375696
|
|
| MD5 |
b481af1661b8bdd28558fc0443583190
|
|
| BLAKE2b-256 |
72a412235e087d2b307ccee7884213ebd8342323e582a74c5b6c751e27116af7
|
Provenance
The following attestation bundles were made for dj_corekit-0.3.0-py3-none-any.whl:
Publisher:
release.yml on himkit/dj-corekit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_corekit-0.3.0-py3-none-any.whl -
Subject digest:
42bf52d02da7575f68b6d1c23c2d0a415b326122220dd3ca8756da4177375696 - Sigstore transparency entry: 2114895623
- Sigstore integration time:
-
Permalink:
himkit/dj-corekit@79e4c040839db01145777b30cb7b18cea5c286ba -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/himkit
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@79e4c040839db01145777b30cb7b18cea5c286ba -
Trigger Event:
release
-
Statement type: