Django Include and Extends Tags in Django Template language

Django Include and Extends Tags copy
Django Django Tutorials

Django Include and Extends Tags in Django Template language

Django Include and Extends Tags in Django Template language

We are learning the Django web framework that is python based. In which we can develop high-quality web applications for different fields and purposes. In Django tutorials, you will learn about DTL which means Django template language, there are different topics you have to learn in DTL as variables, tags, filters, etc.

When we are creating a complete website with Django then we take advantage of DTL because it helps the developer to get target goals. We have published a tutorial on Django template language you can get to learn more. So, in this tutorial, we have to learn about Django Include and Extends tags which are mostly used while developing a Django project. There is some difference between them that we will learn in this article.

Actually, DTL is the way to write python logic in an HTML template in such a way that it should be easily rendered to show the result on the browser. You know, you cannot use programming languages logics directly in an HTML template, but if you want to use programming language, then there are some other ways you have to follow to complete your project requirements. One of them, DTL works like a bridge between HTML, CSS, and programming languages in this case it is python.

What is Django include tag and How to Use?

The include is used in other languages also while developing websites or web applications like PHP. In PHP you have to include a template in another template. Same like that, Django includes tag is also used to include one template in other but you have to render one template in the views function. For example, you an index template web page having different sections like header, nav, body, footer.  Instead of writing code in an HTML file, you can create four different HTML files for different sections. Then you can include all sections in one main template file that is the index file. It will clear your code to understand or to manage.

So, it is the same concept in Django while designing template files.  You can see before syntax in Django.

Syntax

To use Django include tag you have to use {% include %} tag.

{% include “TemplateName.html” %}

Example

{% include “templates/nav.html” %

What is Django extends tag and How to Use it

To use an extended tag, you need to use a block in your main template, which you want to extend. Extends mean, you did not want to write again and again code. To avoid writing the same code we extend a template. Extend means, you are inheriting template sections.

We discuss a example to understand Django extends tag proplery.

After creating a project and applicaiton, you have to write view function: jafri/views.py,

views.py

# import Http Response from django
from django.shortcuts import render

# create a function
def jafri_view(request):

# return response
return render(request, “jafriextends.html”)

urls.py

from django.urls import path

# importing views from views.py
from .views import jafri_view

urlpatterns = [
path(”, jafri_view),
]

You have to create a templates folder, you should know, if you want to extend a template, you should use block tags.

templates/jafri.html.

<h1>Jafri Template</h1>

{% block content %}
{% endblock content %}

You have to create a template file named jafriextends.html

The following template with the name jafriextends.html is extended because we have used another template jafri.html in it. All the elements should be common in both templates but the content of the block tag will be different. The purpose of using extends tag is to avoid using of same code again and again. You have notices that we did not use the same code of jafr.html because we just extend that template to include other content.

{% extends “jafri.html” %}

{% block content %}
<h2> JafriCode is the best platform to learn Web Development, Programming languages, Web Programmings</h2>
{% endblock %}

Conclusion

We have discussed Django Include and Extends tags. I am sure, you have learned from this Django tutorial that how to use Django Include and Extends Tags in your Django Template.

If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible and we try to produce quality content for our readers.

If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.

Recommended Django Tutorial for Beginners

How to Setup Django Static files in Django Project and Application

Complete Guide on Django ORM | Django Tutorials

What Programming Language is used for Data Analytics

What is Django Middleware | Conceptual Guide | Django Tutorials

Complete Built-in Django Form Fields with Examples | Django Tutorials

Leave your thought here

Your email address will not be published. Required fields are marked *

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare

Get your Enrollment

50%OFF

Leave your details below and receive a discount coupon in your inbox