### Install django-template-partials Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Install the package using pip. Then, add 'template_partials' to your Django project's INSTALLED_APPS. ```bash pip install django-template-partials ``` ```python INSTALLED_APPS = [ "template_partials", ... ] ``` -------------------------------- ### Set Up Virtual Environment and Install Dependencies Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Commands to create a Python virtual environment, activate it, and install the project dependencies along with testing requirements. ```sh python -m venv .venv source .venv/bin/activate python -m pip install -e .[tests] ``` -------------------------------- ### Use SimpleAppConfig for Manual Loader Configuration Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md When using SimpleAppConfig, you must manually configure the template loader. Add this to your INSTALLED_APPS if you need fine-grained control over template loading. ```python INSTALLED_APPS = [ "template_partials.apps.SimpleAppConfig", ... ] ``` -------------------------------- ### Add template_partials to INSTALLED_APPS Source: https://context7.com/carltongibson/django-template-partials/llms.txt Add 'template_partials' to your INSTALLED_APPS in settings.py to enable the template loader functionality. ```python INSTALLED_APPS = [ "template_partials", "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", ] ``` -------------------------------- ### Run Tests with Coverage Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Generate a code coverage report for the project's test suite using the 'just' command runner. ```sh just coverage ``` -------------------------------- ### Define and Include a Django Template Partial Source: https://github.com/carltongibson/django-template-partials/blob/main/tests/templates/included.html Demonstrates how to define a reusable template partial using `partialdef` and then include it in another template using `partial`. Ensure the `partials` template tag is loaded. ```django {% load partials %} INCLUDED TEMPLATE START {% partialdef included-partial %} THIS IS CONTENT FROM THE INCLUDED PARTIAL {% endpartialdef %} Now using the partial: {% partial included-partial %} INCLUDED TEMPLATE END ``` -------------------------------- ### Run Tests with Just Command Runner Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Execute the project's test suite using the 'just' command runner. This is a convenient way to run predefined tasks. ```sh just test ``` -------------------------------- ### Define and Extend Template Partials in Django Source: https://github.com/carltongibson/django-template-partials/blob/main/tests/templates/child.html Demonstrates how to define a partial with 'partialdef' and include it in a base template that extends another. Ensure the 'partials' tag is loaded. ```html {% extends 'base.html' %} {% load partials %} {% partialdef extra-content %} Extra Content {% endpartialdef %} {% block main %} Main Content {% endblock %} ``` -------------------------------- ### Clone the Repository Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Standard Git command to clone the project repository for local development or contribution. ```sh git clone git@github.com:your-username/django-template-partials.git ``` -------------------------------- ### Inline Partials with Immediate Rendering Source: https://context7.com/carltongibson/django-template-partials/llms.txt Use the `inline` argument with `partialdef` to render the partial content immediately where defined, while still allowing it to be reused later. ```html {% load partials %}
{% partialdef content-section inline %}

{{ section.title }}

{{ section.body }}

{% endpartialdef %}
``` -------------------------------- ### Render Partial via Template Loader Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Pass a template path with a partial name appended (e.g., 'example.html#test-partial') to the template loader to render only that specific partial. ```python # In view handler… self.template_name = "example.html#test-partial" ``` -------------------------------- ### Define and Use Partials with partialdef and partial Tags Source: https://context7.com/carltongibson/django-template-partials/llms.txt Use the `partialdef` tag to define a reusable template fragment and the `partial` tag to render it. Partials can be defined anywhere and used multiple times. ```html {% load partials %} {% partialdef user-card %}

{{ user.name }}

{{ user.email }}

{% endpartialdef %}
{% for user in users %} {% partial user-card %} {% endfor %}
``` -------------------------------- ### Advanced Configuration with SimpleAppConfig Source: https://context7.com/carltongibson/django-template-partials/llms.txt Use `SimpleAppConfig` in `settings.py` to prevent automatic loader configuration for fine-grained control. Manually wrap loaders for specific template engines. ```python # settings.py INSTALLED_APPS = [ "template_partials.apps.SimpleAppConfig", # No automatic loader setup # ... other apps ] # Manual loader configuration from template_partials.apps import wrap_loaders TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", "NAME": "custom_engine", "DIRS": [BASE_DIR / "templates"], "OPTIONS": { "context_processors": [ "django.template.context_processors.debug", "django.template.context_processors.request", ], }, }, ] # Apply partials loader to specific engine wrap_loaders("custom_engine") ``` -------------------------------- ### Context Handling with Partials Source: https://context7.com/carltongibson/django-template-partials/llms.txt Partials render with the current template context, working naturally within loops and with the `with` tag for context modifications. ```html {% load partials %} {% partialdef product-item %}
{{ product.name }}

{{ product.name }}

${{ product.price }} {% if show_description %}

{{ product.description }}

{% endif %}
{% endpartialdef %}
{% for product in products %} {% partial product-item %} {% endfor %}
``` -------------------------------- ### Load Partials via Template Loader Source: https://context7.com/carltongibson/django-template-partials/llms.txt Use the `template_name#partial_name` syntax to load specific partials directly via the template loader, useful for returning only partial content from views. ```python # views.py from django.views.generic import TemplateView from django.template.loader import render_to_string from django.http import HttpResponse class UserCardView(TemplateView): # Load only the user-card partial from users.html template_name = "users.html#user-card" def htmx_user_card(request, user_id): user = User.objects.get(pk=user_id) # Render just the partial, not the full template html = render_to_string("users.html#user-card", {"user": user}) return HttpResponse(html) ``` -------------------------------- ### Define and Use an Inline Partial Source: https://github.com/carltongibson/django-template-partials/blob/main/tests/templates/example.html Define an inline partial using `partialdef` with the `inline` argument. This partial can be rendered directly where it is defined. ```html {% partialdef inline-partial inline %} INLINE-CONTENT {% endpartialdef %} ``` -------------------------------- ### Include Partial using Include Tag Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md The 'include' tag can also be used with the special '#partial-name' syntax to render a specific partial from a template. ```html+django {% include "example.html#test-partial" %} ``` -------------------------------- ### Include Partials from Other Templates Source: https://context7.com/carltongibson/django-template-partials/llms.txt Include partials from other templates using Django's `include` tag with the `#partial_name` syntax. ```html {% load partials %} {% partialdef alert-box %}
{{ alert_message }}
{% endpartialdef %} {% with alert_type="success" alert_message="Operation completed!" %} {% include "base_components.html#alert-box" %} {% endwith %} ``` -------------------------------- ### Using Partials with Template Inheritance Source: https://context7.com/carltongibson/django-template-partials/llms.txt Define and use partials within child templates that extend a base template. Ensure the `partials` tag library is loaded. ```html {% block title %}{% endblock %} {% block main %}{% endblock %} {% extends 'base.html' %} {% load partials %} {% partialdef sidebar-widget %}
{{ widget_title }}
{{ widget_content }}
{% endpartialdef %} {% block main %}
{{ article.body }}
{% endblock %} ``` -------------------------------- ### Define a Reusable Template Partial Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Load the 'partials' tags and define a named partial using 'partialdef'. The content between 'partialdef' and 'endpartialdef' will be the partial's body. ```html {% load partials %} {% partialdef test-partial %} TEST-PARTIAL-CONTENT {% endpartialdef %} ``` ```html {% load partials %} {% partialdef test-partial %} TEST-PARTIAL-CONTENT {% endpartialdef test-partial %} ``` -------------------------------- ### Returning Multiple Partials in a Single Response Source: https://context7.com/carltongibson/django-template-partials/llms.txt Combine multiple partials into a single HTTP response for HTMX out-of-band swaps. Several strategies are provided for rendering and combining partials. ```python # views.py from django.http import HttpResponse from django.template.loader import render_to_string def update_dashboard(request): context = { "stats": get_updated_stats(), "notifications": get_notifications(), } template_partials = [ "dashboard.html#stats-widget", "dashboard.html#notifications-widget", ] # Strategy 1: Combine rendered partials response_content = "" for template_name in template_partials: response_content += render_to_string(template_name, context) return HttpResponse(response_content) # Strategy 2: Use response as file-like object response = HttpResponse() for template_name in template_partials: response.write(render_to_string(template_name, context)) return response # Strategy 3: Use generator expression return HttpResponse( render_to_string(template_name, context) for template_name in template_partials ) ``` -------------------------------- ### Manually Define Template Loaders with Partials Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Explicitly define the template loaders, including the partials loader, within the TEMPLATES setting. This provides the most control over the loading order and mechanism. ```python 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)] partial_loaders = [("template_partials.loader.Loader", cached_loaders)] settings.TEMPLATES[...]['OPTIONS']['loaders'] = partial_loaders ``` -------------------------------- ### Define and Render a Django Template Partial Source: https://github.com/carltongibson/django-template-partials/blob/main/tests/templates/debug.html Use `partialdef` to define a reusable template partial and `partial` to render it within your Django templates. The `{{ exception }}` variable can be used to display any exceptions that occur during partial rendering. ```django {% load partials %} {% partialdef test-partial %} {{ exception }} {% endpartialdef %} {% block main %} {% partial test-partial %} {% endblock main %} ``` -------------------------------- ### Control Context with 'with' Tag Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md When rendering a partial within a loop or other context-sensitive block, use the 'with' tag as usual to adjust the context passed to the partial. ```html+django {% for object in object_list %} {% partial test-partial %} {% endfor %} ``` ```html+django {% with name=value othername=othervalue %} {% partial test-partial %} {% endwith %} ``` -------------------------------- ### Reuse a Defined Template Partial Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Use the 'partial' tag to render a previously defined named partial multiple times within your template. ```html {% block main %} BEGINNING {% partial test-partial %} MIDDLE {% partial test-partial %} END {% endblock main %} ``` -------------------------------- ### Uninstall django-template-partials package Source: https://github.com/carltongibson/django-template-partials/blob/main/Migration.md Uninstall the django-template-partials package using pip and remove it from your dependency files. ```bash pip uninstall django-template-partials ``` -------------------------------- ### Define an Inline Partial Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Use the 'inline' argument in 'partialdef' to wrap an existing part of your page and continue rendering content within the partial. ```html {% block main %} {% partialdef inline-partial inline %} CONTENT {% endpartialdef %} {% endblock main %} ``` -------------------------------- ### Add Partial Tags to Template Builtins Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Configure Django's TEMPLATES setting to automatically load partials tags in all templates, avoiding the need for manual {% load partials %} in each file. ```python OPTIONS = { "builtins": ["template_partials.templatetags.partials"], } ``` -------------------------------- ### Adding Partials to Template Builtins Source: https://context7.com/carltongibson/django-template-partials/llms.txt Add `template_partials.templatetags.partials` to Django's template builtins in `settings.py` to avoid needing `{% load partials %}` in every template. ```python # settings.py TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", "DIRS": [BASE_DIR / "templates"], "APP_DIRS": True, "OPTIONS": { "builtins": [ "template_partials.templatetags.partials", ], "context_processors": [ "django.template.context_processors.debug", "django.template.context_processors.request", ], }, }, ] ``` ```html {% partialdef my-partial %}
Content
{% endpartialdef %} {% partial my-partial %} ``` -------------------------------- ### Define and Use a Named Partial Source: https://github.com/carltongibson/django-template-partials/blob/main/tests/templates/example.html Use `partialdef` to define a named partial and `partial` to render it within your Django templates. This is useful for creating reusable blocks of content. ```html {% load partials %} {% partialdef test-partial %} TEST-PARTIAL-CONTENT {% endpartialdef %} {% block main %} BEGINNING {% partial test-partial %} MIDDLE {% partial test-partial %} END {% endblock main %} ``` -------------------------------- ### Load Partial Tags in Templates Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Use this tag to load the partials functionality within a specific Django template. ```django {% load partials %} ``` -------------------------------- ### Wrap Template Loaders with Partial Loader Source: https://github.com/carltongibson/django-template-partials/blob/main/README.md Use wrap_loaders to add the partials loader to a specific Django template engine instance identified by its NAME. This allows for targeted configuration. ```python from template_partials.apps import wrap_loaders TEMPLATES = [ ..., { "BACKEND": "...", "NAME": "myname", "OPTIONS": { ..., }, }, ..., ] wrap_loaders("myname") ``` -------------------------------- ### Named End Tags for Readability Source: https://context7.com/carltongibson/django-template-partials/llms.txt Optionally, provide the partial name to the `endpartialdef` tag for improved readability in complex templates. ```html {% load partials %} {% partialdef navigation-menu %} {% endpartialdef navigation-menu %}
{% partial navigation-menu %}
``` -------------------------------- ### Remove from INSTALLED_APPS Source: https://github.com/carltongibson/django-template-partials/blob/main/Migration.md Remove the 'template_partials' entry from your Django project's INSTALLED_APPS setting. ```python # Before INSTALLED_APPS = [ "template_partials", # Remove this line "django.contrib.admin", # ... other apps ] # After INSTALLED_APPS = [ "django.contrib.admin", # ... other apps ] ``` -------------------------------- ### Remove TEMPLATES builtins configuration Source: https://github.com/carltongibson/django-template-partials/blob/main/Migration.md Remove 'template_partials.templatetags.partials' from the TEMPLATES builtins list if manually configured. ```python # Remove from TEMPLATES OPTIONS if present OPTIONS = { "builtins": [ "template_partials.templatetags.partials", # Remove this line ], } # Remove custom loader configurations if you used wrap_loaders() or manual setup # loaders = [("template_partials.loader.Loader", cached_loaders)] # Remove ``` -------------------------------- ### Remove {% load partials %} template tag Source: https://github.com/carltongibson/django-template-partials/blob/main/Migration.md Remove the '{% load partials %}' tag from your HTML templates as it is now built-in. ```html {% load partials %} {% partialdef my-partial %} {% endpartialdef %} {% partialdef my-partial %} {% endpartialdef %} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.