Reusable block fragments for the Django Template Language.
Project description
django-block-fragments
Render only the content of a specific block of a Django template. This also works for arbitrary template inheritance or when the block is in an included template.
Rendering only a part of a template is especially useful when using Django together with libraries like HTMX, see Template Fragments.
Installation
Install with pip:
pip install django-block-fragments
Or with uv:
uv add django-block-fragments
Then add block_fragments to INSTALLED_APPS:
INSTALLED_APPS = [
...,
"block_fragments",
...,
]
[!NOTE]
django-block-fragmentscurrently only supports the Django template backend.
See Advanced configuration (below) for more options.
Usage
Once installed and having a template like this:
...
{% block content %}
Some content
{% endblock content %}
...
You can render just the "content" block in a view with:
from django.shortcuts import render
def my_view(request):
return render(request, "template.html#content", {})
You can also include just the "content" block in another template:
{% include "template.html#content" %}
Advanced configuration
By default, adding "block_fragments" to your INSTALLED_APPS will try to configure any Django template backend to use the block fragments template loader.
If you need to control this behavior, you can use the alternative SimpleAppConfig, which will not adjust your TEMPLATES setting:
INSTALLED_APPS = [
"block_fragments.apps.SimpleAppConfig",
...,
]
If you use SimpleAppConfig, you will need to configure the template loader yourself.
A wrap_loaders() function is available, and can be used to configure any specific template engine instance with the block fragments loader.
You can use the backend's NAME to wrap_loaders() to add the block fragments loader just for that backend:
from block_fragments.apps import wrap_loaders
TEMPLATES = [
...,
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"NAME": "myname",
"OPTIONS": {
...,
},
},
...,
]
wrap_loaders("myname")
If the NAME isn't provided, the penultimate element of the BACKEND value is used - for example, "django.template.backends.django.DjangoTemplates" would be equivalent to a NAME of "django".
Under the hood, wrap_loaders() is equivalent to explicitly defining the loaders by-hand. Assuming defaults…
from django.conf import settings
default_loaders = [
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
]
cached_loaders = [("django.template.loaders.cached.Loader", default_loaders)]
block_fragment_loaders = [("block_fragments.loader.Loader", cached_loaders)]
settings.TEMPLATES[...]['OPTIONS']['loaders'] = block_fragment_loaders
… where TEMPLATES[...] is the entry in TEMPLATES with the NAME matching
that passed to wrap_loaders().
Development
Fork, then clone the repo:
git clone git@github.com:your-username/django-block-fragments.git
Install dependencies (needs uv to be installed):
uv sync
Then you can run the tests by using pytest:
uv run pytest
Or with coverage:
uv run pytest --cov
Acknowledgements
This project is heavily inspired and uses code from django-template-partials by Carlton Gibson and django-render-block by Patrick Cloke. So a big thank you to them!
FAQ
Why django-block-fragments when django-template-partials and django-render-block already exist?
I was looking for a way to reuse the already existing block tags of the Django Template Language (like django-render-block does) but also wanted to have the convenience of using template loaders (like django-template-partials does). So django-block-fragments tries to combine features of both of these great projects.
How to use django-block-fragments with django-cotton?
When using django-block-fragments together with django-cotton the automatic loader configuration won't work (as both would overwrite each other). So you must use the SimpleAppConfig and configure the template loaders manually like in the example below.
INSTALLED_APPS = [
"django_cotton.apps.SimpleAppConfig",
"block_fragments.apps.SimpleAppConfig",
]
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"OPTIONS": {
"loaders": [
(
"block_fragments.loader.Loader",
[
(
"django.template.loaders.cached.Loader",
[
"django_cotton.cotton_loader.Loader",
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
)
],
)
],
"context_processors": [
# no changes
],
"builtins": [
"django_cotton.templatetags.cotton",
],
},
},
]
[!NOTE] Because we're specifying the loaders manually, Django's APP_DIRS setting no longer has any effect. If you still want to load templates from the apps automatically, make sure to add the
django.template.loaders.app_directories.Loaderas in the example above.
License
MIT License
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 django_block_fragments-0.1.1.tar.gz.
File metadata
- Download URL: django_block_fragments-0.1.1.tar.gz
- Upload date:
- Size: 22.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
207e9a8c76e1b52486ddf8ee1bdc5aad4f65f3d1a0874b5303ad1b6d08ef0850
|
|
| MD5 |
f60d59459d682c581ec744b28982a5e0
|
|
| BLAKE2b-256 |
054db90af5ed02efed97232b1bb3198146de01beee35be478223a092c7bc1fea
|
Provenance
The following attestation bundles were made for django_block_fragments-0.1.1.tar.gz:
Publisher:
publish.yml on medihack/django-block-fragments
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_block_fragments-0.1.1.tar.gz -
Subject digest:
207e9a8c76e1b52486ddf8ee1bdc5aad4f65f3d1a0874b5303ad1b6d08ef0850 - Sigstore transparency entry: 180612893
- Sigstore integration time:
-
Permalink:
medihack/django-block-fragments@43bfe52dac500e01ddaae568bc67f77f17a99454 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/medihack
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@43bfe52dac500e01ddaae568bc67f77f17a99454 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_block_fragments-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_block_fragments-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f1a24a18da95f0d2eb4db4a402755488535e14bc3e545fe8c0ab33043c94261
|
|
| MD5 |
4b6d50441c1946774676f91444c50fd6
|
|
| BLAKE2b-256 |
e74ca515e1b4f6a63785c73d9da5d5eceae21157d95cea09748d196029db4ee4
|
Provenance
The following attestation bundles were made for django_block_fragments-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on medihack/django-block-fragments
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_block_fragments-0.1.1-py3-none-any.whl -
Subject digest:
9f1a24a18da95f0d2eb4db4a402755488535e14bc3e545fe8c0ab33043c94261 - Sigstore transparency entry: 180612894
- Sigstore integration time:
-
Permalink:
medihack/django-block-fragments@43bfe52dac500e01ddaae568bc67f77f17a99454 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/medihack
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@43bfe52dac500e01ddaae568bc67f77f17a99454 -
Trigger Event:
release
-
Statement type: