Small Django utilities built on Django core.
Project description
dj-corekit
Small Django utilities built on Django core.
Install
pip install dj-corekit
Cache Page
Function view:
from dj_corekit.cache import cache_page
@cache_page(
timeout=300,
key_func=lambda request: f"product:{request.GET.get('id')}",
headers=lambda request, response: {
"X-Cache-Scope": "product",
"Cache-Control": "public, max-age=300",
},
)
def product_detail(request):
...
Class-based view with Django's method_decorator:
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):
...
Class-based view with the mixin:
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']}"
key_func returns the full Django cache key. The response is stored through
django.core.cache.caches[cache_alias].
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.
API
cache_page(
timeout,
key_func,
*,
headers=None,
cache_alias="default",
only_if=None,
)
class CachePageMixin:
cache_timeout = 300
cache_headers = None
cache_alias = "default"
cache_only_if = None
def get_cache_key(self, request):
...
timeout: seconds, or a callable receiving the response.key_func: 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.cache_alias: Django cache alias, default"default".only_if: callable receiving the request. ReturnFalseto bypass cache.
Development
python -m pip install -e ".[dev]"
python -m pytest
ruff check .
ruff format --check .
tox
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.1.0.tar.gz.
File metadata
- Download URL: dj_corekit-0.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb7a8c7555d2542fb6b9f6e5693b55d0ec3431706b635c4ff9cbb7aedf176de9
|
|
| MD5 |
3af0ae01d543da742e0d7e38f8552853
|
|
| BLAKE2b-256 |
124448dae0f614354cb3250e33fa4b2045bc076f5b4f455fe492bbaec54f85c7
|
File details
Details for the file dj_corekit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dj_corekit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
568f30ecbf61bd02aef879a8b2b727cfe55bc931345cea8dc42dedcf4569aac7
|
|
| MD5 |
0923acffbfd133c224543c15e5315522
|
|
| BLAKE2b-256 |
cc55240cd2232f9d548150f6864aceb33a50aaa7fd05a9defb3b72a0a4313536
|