• Stars
    star
    443
  • Rank 98,504 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 1 year ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reusable named inline partials for the Django Template Language.

django-template-partials

Reusable named inline partials for the Django Template Language.

Watch the talk

This is the django-template-partials package I discussed in my DjangoCon Europe 2023 talk in Edinburgh.

For a quick introduction, you can watch the video on YouTube. 🍿

DjangoCon Europe 2023 | Yak-shaving to Where the Puck is Going to Be.

Installation

Install with pip:

pip install django-template-partials

Then set up your project:

# Install app and configure loader.
default_loaders = [
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
]
cached_loaders = [("django.template.loaders.cached.Loader", default_loaders)]
partial_loaders = [("template_partials.loader.Loader", cached_loaders)]
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        # Comment this out when manually defining loaders.
        # "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
            "debug": True,
            # TODO: Add wrap_loaded function to the called from an AppConfig.ready().
            "loaders": partial_loaders,
        },
    },
]
INSTALLED_APPS = [
    "template_partials",
    ...,
]

Usage

Load the partials tags and define a re-usable partial at the top of your template:

{% load partials %}

{% startpartial test-partial %}
TEST-PARTIAL-CONTENT
{% endpartial %}

Then later you can reuse it:

{% block main %}
BEGINNING
{% partial test-partial %}
MIDDLE
{% partial test-partial %}
END
{% endblock main %}

django-template-partials is also integrated with the template loader, so you can pass a template plus a partial name to the loader to have just that part rendered:

self.template_name = "example.html#test-partial"

The rest of your view logic remains the same.

Documentation

Fuller docs and write up still COMING SOON, but the talk explains most of it.

Enjoy! πŸš€

Running the tests

Fork, then clone the repo:

git clone [email protected]:your-username/django-template-partials.git

Set up a venv:

python -m venv .venv
source .venv/bin/activate
python -m pip install -e .[tests]

Then you can run the tests with the just command runner:

just test

Or with coverage:

just coverage

If you don't have just installed, you can look in the justfile for a commands that are run.

More Repositories

1

django-filter

A generic system for filtering Django QuerySets based on user selections
Python
4,436
star
2

neapolitan

Quick CRUD views for Django
Python
480
star
3

django-unique-user-email

Enable login-by-email with the default User model for your Django project by making auth.User.email unique.
Python
116
star
4

django-sphinx-view

Django Powered Dynamic Sphinx Docs.
Python
51
star
5

djangocon-sprints

Notes for "Getting started contributing to Django" sprints workshop.
28
star
6

Noumenal-PHP-Library

PHP Source and General Resources
PHP
20
star
7

azure-functions-wsgi-adapter

Adapter to run your WSGI application in Azure Functions. πŸŽ‰ NO LONGER NEEDED β€”Β Azure Functions Python SDK DOES THIS ITSELF NOW.
Python
9
star
8

notes

Being a (Django) open source maintainer discussion group, and my notes on misc.
Python
6
star
9

JavaScript

General resources
Python
5
star
10

django-staticsite

Django static site generator for DEBUG and production
Python
3
star
11

site-starter

Port of HTML5 Boilerplate to Django templates, plus static site generator.
JavaScript
2
star
12

CouchDBX-Redux

Work in progress repo for resurrection of CouchDBX β€”Β A Mac OS X wrapper for CouchDBX
Objective-C
2
star
13

django-http-benchmarks

A Django project for investigating HTTP performance in various scenarios.
Python
1
star
14

Holiday-Activity

Christmas Tree commit activity from Working Copy.
1
star
15

django_ticket_32539

Python
1
star
16

YouTubePortal

Reusable wrapper around the YouTube mobile site, with options to open in YouTube app and Mobile Safari
Objective-C
1
star