Skip to main content

Reusable block fragments for the Django Template Language.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_block_fragments-0.1.1.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_block_fragments-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

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

Hashes for django_block_fragments-0.1.1.tar.gz
Algorithm Hash digest
SHA256 207e9a8c76e1b52486ddf8ee1bdc5aad4f65f3d1a0874b5303ad1b6d08ef0850
MD5 f60d59459d682c581ec744b28982a5e0
BLAKE2b-256 054db90af5ed02efed97232b1bb3198146de01beee35be478223a092c7bc1fea

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_block_fragments-0.1.1.tar.gz:

Publisher: publish.yml on medihack/django-block-fragments

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_block_fragments-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_block_fragments-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f1a24a18da95f0d2eb4db4a402755488535e14bc3e545fe8c0ab33043c94261
MD5 4b6d50441c1946774676f91444c50fd6
BLAKE2b-256 e74ca515e1b4f6a63785c73d9da5d5eceae21157d95cea09748d196029db4ee4

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page