Skip to main content

django-block-fragments

pypi

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-fragments currently 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.Loader as in the example above.

License

MIT License

Release files for django-block-fragments 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-block-fragments 0.1.1
File Size Uploaded
django_block_fragments-0.1.1.tar.gz 22.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-block-fragments 0.1.1
File Interpreter ABI Platform
django_block_fragments-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 30.4 kB

Release files / django_block_fragments-0.1.1.tar.gz

Download URL django_block_fragments-0.1.1.tar.gz
Size 22.7 kB
Tags Source
SHA-256 checksum
How to use checksums
207e9a8c76e1b52486ddf8ee1bdc5aad4f65f3d1a0874b5303ad1b6d08ef0850
BLAKE2b-256 checksum
How to use checksums
054db90af5ed02efed97232b1bb3198146de01beee35be478223a092c7bc1fea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 12, 2025.

Transparency log

Release files / django_block_fragments-0.1.1-py3-none-any.whl

Download URL django_block_fragments-0.1.1-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f1a24a18da95f0d2eb4db4a402755488535e14bc3e545fe8c0ab33043c94261
BLAKE2b-256 checksum
How to use checksums
e74ca515e1b4f6a63785c73d9da5d5eceae21157d95cea09748d196029db4ee4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 12, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page