39π
The most flexible way to reuse template fragments is to define an inclusion_tag. You can pass arguments to your custom tag, process them a bit in Python, then bounce back to a template. Direct inclusion only works for fragments that donβt depend on the surrounding context.
Quick example from the docs:
In app/templatetags/poll_extras.py
register the tag with a decoration:
from django import template
register = template.Library()
@register.inclusion_tag('results.html')
def show_results(poll):
choices = poll.choice_set.all()
return {'choices': choices}
In app/templates/results.html
:
<ul>
{% for choice in choices %}
<li> {{ choice }} </li>
{% endfor %}
</ul>
Calling the tag:
{% load poll_extras %}
{% show_results poll %}
- [Django]-Installing a django site on GoDaddy
- [Django]-Django "can't set attribute" in model
- [Django]-Django Broken pipe in Debug mode
11π
If you need to use {% block %}
you can only do that via the {% extend %}
approach. Otherwise, you can use {% include 'some.html' %}
to include a bit of HTML in multiple places.
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-How to override the verbose name of a superclass model field in django
- [Django]-How can I test https connections with Django as easily as I can non-https connections using 'runserver'?
4π
The unofficial Django Reusable App Conventions recommends using these block names:
{% block title %}
{% block extra_head %}
{% block body %}
{% block menu %}
{% block content %}
{% block content_title %}
{% block header %} {% block footer %}
{% block body_id %} {% block body_class %}
{% block [section]_menu %} {% block page_menu %}
If everyone stuck to these conventions, it should make this problem easier. Follow the link to see the description of each block.
- [Django]-Django unit testing with date/time-based objects
- [Django]-How to iterate over nested dictionaries in django templates
- [Django]-How do I keep my Django server running even after I close my ssh session?
2π
Example of using {% include %}
tag
- All data comes from Django back-end
- Many values are passed to
card_template.html
usinginclude
tag inpage1.html
card_template.html
<style>
.choices_div {
border-radius: 5rem;
}
.card-footer {
background-color: transparent;
border: transparent;
}
</style>
<div class="col mb-5 px-4">
<div class="card h-100 w-100 jumbotron choices_div {{ bg_color|default:'' }}">
<div class="card-body p-0">
<h3 class="card-title text-center">{{ card_title|capfirst }}</h3>
<ul class="card-text mt-3">
{% for c in card_body_list %}
<li>{{ c }}</li>
{% endfor %}
</ul>
</div>
<div class="card-footer text-center pt-4">
{% if get_post_request == 1 %}
<a class="btn btn-light" href="{{ href }}">{{ button_text }}</a>
{% else %}
<form method="post">
{% csrf_token %}
<button type="submit" class="btn btn-light w-75" name="category"
value="{{ button_value }}">{{ button_text }}</button>
</form>
{% endif %}
</div>
</div>
</div>
page1.html
{% extends 'core/core.html' %}
{% block body %}
<div class="jumbotron bg-white">
<div class="container">
<div class="mb-5 text-center">
<h1>Choose user category</h1>
<h5>Once choosen, the user category cannot be changed</h5>
</div>
<div class="row row-cols-lg-2 justify-content-around">
{% for object in object_list %}
{% cycle 'bg_peac**k' 'bg_sunset' 'bg_skin' 'bg_brown' as bg_color silent %}
{% include 'core/card_template.html' with card_title=object.category card_body_list=object.description get_post_request=2 button_text='Select' bg_color=bg_color button_value=object.id %}
{% endfor %}
</div>
</div>
</div>
{% endblock %}
- [Django]-Django ImageField change file name on upload
- [Django]-Django + Ajax
- [Django]-Single Django model, multiple tables?
1π
As other answers have mentioned, the simplest approach is direct inclusion:
{% include 'mytemplate.html' %}
It is possible to modify the context of the rendered template (Or in simpler terms, to pass variables to the template) using
{% include 'mytemplate.html' with poll=poll %}
To use the traditional polls example, the template I would write would be:
<div class="stylish-poll">
{% for choice in poll.choices %} <!-- poll is a template variable -->
{% include 'choice_template.html' with choice=choice %}
{% endfor %}
</div>
Another potentially useful thing to know is that the only
keyword prevents the template variable poll
being passed into 'choice_template.html'
which it would be by default. If you do not want the choice template to have access to {{ poll }}
then the include statement looks like:
{% include 'choice_template.html' with choice=choice only %}
Documentation: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include
- [Django]-Laravel's dd() equivalent in django
- [Django]-How to combine multiple QuerySets in Django?
- [Django]-Django Forms: if not valid, show form with error message
0π
AΓ―e, my fault β the answer is given in the Django Reference (and not discussed in the aforementioned Django Template Documentation)β¦
So: Just use {% include sub_template_name %}
.
- [Django]-Validators = [MinValueValidator] does not work in Django
- [Django]-How to add a new field to a model with new Django migrations?
- [Django]-Django β how to detect test environment (check / determine if tests are being run)
0π
even though the question is asked years ago, any way I will show you the method that worked for me.
base.html
In your base template you need to define all of your blocks that you need to reuse in your other templates,
<html>
<head>
<meta name="description" content="{%block description%}{%endblock%}">
<meta name="keywords" content="{%block keywords%}{%endblock%}">
<title>{%block title%}{%endblock%}</title>
</head>
<body>
<!---other body stuff--->
{%block content%}
{%endblock%}
</body>
</html>
home.html
{%extends 'base.html'%}
<!--you can reuse all blocks here-->
{%block description%}Django reusable blocks, for every bage{%endblock%}
{%block keywords%}django,block, resuable,meta,title,{%endblock%}
{%block title%}django reuseable blocks for title, meta description and meta keywords{%endblock%}
{%block content%}
<div>
<h1> reuse blocks</h1>
</div>
{%endblock%}
- [Django]-Bad Django / uwsgi performance
- [Django]-Django-debug-toolbar not showing up
- [Django]-Can't connect to local MySQL server through socket '/tmp/mysql.sock