Users Online

· Guests Online: 58

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Articles Hierarchy

Django Tutorial

Django Include Template

Django Include Template

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 include 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 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 programming logic like if statement and for loop, 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 comments over the section.
  • cycle: It is used to determine 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, 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.

<!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.

Django Include Template 1

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 2

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 result is shown in the below screenshot.

Example 3

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

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 result is shown in the below screenshot.

image

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.

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.72 seconds
10,842,259 unique visits