Definition of Django Include Template
We know that Django provides different types of features to the user, including one of the features that are provided by Django. With the help of include template, we can generate dynamic web pages, basically, the template contains static and dynamic pages. Normally include is one of the tags that we can use inside the template, by using the include tag we can load the template 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. For the naming convention, we can use a variable name as a template name.
What is Django Include Template?
A Django design is a text record or a Python string expanded using the Django design language. Django being 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, yet moreover give a couple of confined components of programming 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 designs inside an organization. The format name can either be a variable or a hard-coded (referred to) string, in either single or twofold explanations.
Sentence structure
{% incorporate “Specified template_name.html” %}
A capacity that takes a full Python import way to one more URLconf module that ought to be “included” here. Alternatively, the application namespace and case namespace where the passages will be incorporated can likewise be determined.
Normally, the application namespace ought to be indicated by the included module. In the event that 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 as follows.
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 as well as some extraordinary linguistic structure portraying how dynamic substance will be embedded.
Django include tag
Now let’s include the tag in Django as follows.
When we want to implement some programming logic like if statement and for loop then we need to include a tag in the template. 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 the comment over the section.
- cycle: It is used to specify the content of each cycle of the loop and it also depends on the user.
- debug: It is used to show debugging information.
- extends: It is used 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, and 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 need to create an HTML file and write the following code as follows.
!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 that is the name of the student, 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 make changes to 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 as 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 as follows.
<!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 also but here we call the first.html file by using the include keyword as shown. Another structure of the file is the same. The end result is shown in the below screenshot.
Now let’s see another example with python programming as follows.
First, we need to create an index.py file and follow the code as follows.
from django.shortcuts import render
def indexview(request):
return render(request, "first.html")
Explanation
Here first we need to import the render package, after creating the function 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 end result is shown in the below screenshot.
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
This has been a guide to Django Include Template. Here we discussed the Definition, Introduction, How to use django include template, and examples with code implementation. You can also go through our other suggested articles to learn more –
2 Online Courses | 2 Hands-on Projects | 14+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses