include – Django Template Tags

A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some limited features of programming such as variables, for loops, comments, extends, include etc.
This article revolves about how to use include tag in Templates. include tag loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.

Syntax
{% include "template_name.html" %}
Example
{% include "foo/bar.html" %}

Normally the template name is relative to the template loader’s root directory. A string argument may also be a relative path starting with ./ or ../ as described in the extends tag.

include – Django template Tags Explanation

Illustration of How to use include tag in Django templates using an Example. Consider a project named geeksforgeeks having an app named geeks.

Refer to the following articles to check how to create a project and an app in Django.

Now create a view through which we will access the template,
In geeks/views.py,



filter_none

edit
close

play_arrow

link
brightness_4
code

# import Http Response from django
from django.shortcuts import render
  
# create a function
def geeks_view(request):
  
    # return response
    return render(request, "geeks.html.html")

chevron_right


Create a url path to map to this view. In geeks/urls.py,

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.urls import path
  
# importing views from views.py
from .views import geeks_view
  
urlpatterns = [
    path('', geeks_view),
]

chevron_right


Now we will create three templates to demonstrate include tag. Create a base template in geeks.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
    <!-- Include header -->
    {% include "component1.html" %}
  
    <h4>Body Here</h4>
      
    <!-- Include Footer -->
    {% include "component2.html" %}
</html>

chevron_right


Create two components in templates/component1.html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- component1.html -->
<h2> Header Here </h2>>

chevron_right


and templates/component2.html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- component2.html -->
<h4>Footer here</h4>

chevron_right


Now visit http://127.0.0.1:8000/,

include-Django-Template-Tags

Advanced Usage

one can pass additional context to the template using keyword arguments:

{% include "name_snippet.html" with person="Jane" greeting="Hello" %}



My Personal Notes arrow_drop_up

Software Developer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.