Updated May 22, 2023
Definition of Django Include Template
We know that Django provides different types of features to the user, including one of the features that Django provides. With the help of an included template, we can generate dynamic web pages. The template contains static and dynamic pages. Normally include is one of the tags we can use inside the template. We can load the template using the include tag and render it with the currently available context. That means it allows us to include another template within the specified template as per our requirement. We can use a variable name as a template for the naming convention.
What is Django Include Template?
A Django design is a text record or a Python string expanded using the Django design language. Django, an area of strength for a remembered structure, gives solace to conveying data for a configuration. Django designs not simply grant passing data from view to design but give a couple of confined programming components like variables for circles, comments, extends, consolidate, etc.
Here we endeavor to examine how to use a recall tag for Templates. Consolidate mark stacks in a design and deliver them with a continuous setting. This is a way to deal with “counting” various organizational designs. The format name can either be a variable or a hard-coded (referred to) string in single or twofold explanations.
Sentence structure
{% incorporate "Specified template_name.html" %}
A capacity that takes a complete Python import way to one more URLconf module that ought to be “included” here. Alternatively, the application and case namespace where the passages will be incorporated can be determined.
Normally, the application namespace ought to be indicated by the included module. If an application namespace is set, the namespace contention can be utilized to set an alternate occurrence namespace.
incorporate() likewise acknowledges as a contention either an iterable that profits URL designs or a 2-tuple containing such iterable in addition to the names of the application namespaces.
Different parameters:
- Module – URLconf module (or module name)
- Namespace (str) – Instance namespace for the URL sections being incorporated
- pattern_list – Iterable of way() as well as re_path() occurrences.
- app_namespace (str) – Application namespace for the URL sections being incorporated.
How to use Django include template?
Now let’s see how we can use the Django include template.
Being a web system, Django needs a helpful method for producing HTML progressively. The most widely recognized approach depends on layouts. A layout contains the static pieces of the ideal HTML yield and some extraordinary linguistic structure portraying how dynamic substance will be embedded.
Django include tag
Now let’s include the tag in Django as follows.
We need to include a tag in the template when we want to implement programming logic like if statements and for loops. In Django template tag is nothing but if and for a keyword. For implementation, we need to use the % symbol surrounding them as per our requirement.
We have different types of tag references as follows:
- autoescape: It is used on or off autoescape as per our requirement.
- block: It is used to specify the block section.
- comment: We can provide comments over the section.
- cycle: Use to determine the content of each cycle of the loop and depends on the user.
- debug: Use to show debugging information.
- extends: Use to specify the parent template.
- filter: We can filter all content before the execution.
- for: It is nothing but conditional for a loop.
- if: It is nothing but a conditional if statement.
It also provides the other different tags such as firstof, ifchanged, include, load, lorem, now, regroup, resetcycle, spaceless, templatetag, etc.
Example
Let’s assume we have already created a project, so first, we need to configure the setting.py file, and here we need to provide the directory path and name as per our requirement.
After configuration, we must create an HTML file and write the following code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HOME</title>
</head>
<body>
<h2>This is first Django project</h2>
</body>
</html>
Now load the template and see the below screenshot.
Now we have another variable, the student’s name, so first, we need to change the view.py file as follows.
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
def home(request):
template = loader.get_template('home.html')
name_of_student = {
'studname':'Sachin'
}
return HttpResponse(template.render(name_of_student))
After that, we also need to change the html file as follows.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HOME</title>
</head>
<body>
<h2>This is first Django project</h2>
<h2>Name of Student :{{ 'studname }}</h2>
</body>
</html>
Now see the output below screenshot as follows.
Django Include Template Examples
Now let’s see different examples of tags as follows.
First, we create a sample html file name first.html as follows.
<!DOCTYPE html>
<html>
<body>
<h1>My First HTML Web Page</h1>
<p>Hi Welcome thanks for visit.</p>
</body>
</html>
Now create another html file name as index.html and add the following code.
<!DOCTYPE html>
<html>
<body>
<h1>This is my second index file</h1>
{% include 'first.html '%}
<p>Once again thanks for visiting my site, revisit again .</p>
</body>
</html>
Explanation
In the first.html file, we write simple HTML code and print the message, similar to the second file, but here we call the first.html file by using the include keyword as shown. Another structure of the file is the same. The screenshot below shows the result.
Now let’s see another example with Python programming as follows.
First, we need to create an index.py file and follow the code.
from django.shortcuts import render
def indexview(request):
return render(request, "first.html")
Explanation
First, we need to import the render package. After creating and inside the function, we call another HTML file.
Now create a URL path to map this function as follows.
from django.urls import path
from .index import indexview
urlname= [
path('', indexview),
]
Now everything is ok, create a template to see the result as follows.
Here we created an html file name as home.
<!DOCTYPE html>
<html>
<body>
{% include 'first.html '%}
{% include index.html%}
</body>
</html>
Explanation
Here first.html contains the Hi this “my home page” and index.html contains “Welcome” here. The screenshot below shows the result.
Conclusion
With the help of the above article, we try to learn about the Django include template. From this article, we learn basic things about the Django include template, and we also see the features and installation of the Django include template and how we use it in the Django include template.
Recommended Articles
We hope that this EDUCBA information on “Django Include Template” was beneficial to you. You can view EDUCBA’s recommended articles for more information.