Updated May 22, 2023
Introduction to Django include
The following article provides an outline for Django include. We know that Django offers different types of features to the user, including one of Django’s features. Typically include is one of the tags 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 we can include another template within the specified template per our requirement. For example, for the naming convention, we can use a variable name as a template name, or we can also use a hard-coded string as per our requirement, the hard-coded string either in single or double quotes.
What is Django include?
A Django layout is a text record or a Python string increased utilizing the Django format language. Django, as a robust battery-included framework, provides the convenience of delivering information in a format. In addition, Django layouts permit passing information from view to layout and provide a few restricted programming elements like factors for circles, remarks, expands, incorporate, and so on.
Here we attempt to investigate how to utilize a remember tag for Templates. Incorporate label stacks in a layout and renders them with the ongoing setting. This is an approach to “counting” different layouts inside a format. The layout name can either be a variable or a hard-coded (cited) string in one or the other single or twofold statements.
Sentence structure:
{% incorporate "Specified template_name.html" %}
A capacity that takes a complete Python import way to one more URLconf module ought to be “included” here. Alternatively, the application namespace and case namespace where the passages will be incorporated can likewise be determined. Typically, the application namespace ought to be indicated by the included module. However, 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.
Django include URLs
Now let’s see include URLs as follows:
We know that a URL is nothing but a web address, and we can see in our web browser that when we try opening any web page, we need an URL to access that web page; in Django, we have different patterns to match specified web pages.
Please refer to the below screenshot for the sample URL in Django.
Now let’s see how we can work with URLs in Django as follow:
Django already has a URL configuration file; inside the configuration file, we have space to add user-defined functions per our requirements.
Code:
from django.contrib import admin
from django.urls import path, include
urls = [
path('admin/', admin.site.urls),
path('', include('index.urls')),
]
Explanation:
- In the above code, we can see here we added another URL with the help we included, as shown; now, we need to create another Python file inside the index directory and add the following line.
Code:
from django.urls import path
from . import views
Now we need to set the URL pattern that we want:
Code:
url =[path('', views.list of post, name = 'specified name of post')]
After execution, we get the following error message as shown below screenshot.
Output:
Here we have specified URL patterns inside the index.
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 some programming logic like an if statement and for loop. In Django template tag is nothing but if and for a keyword. So we must use the % symbol surrounding them per our implementation 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 determines the content of each loop cycle and 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, load, lorem, now, regroup, resetcycle, spaceless, templatetag, etc.
Examples of Django include
Now let’s see different examples of tags as follows:
First, we create a sample html file name first.html as follows:
Code:
<!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:
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.
Output:
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:
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 the function and inside the function, we call another HTML file.
Now create a URL path to map this function as follows:
Code:
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.
Code:
<!DOCTYPE html>
<html>
<body>
{% include 'first.html '%}
{% include index.html%}
</body>
</html>
Explanation:
- Here first.html contains Hi this, “my home page,” and index.html contains “Welcome” here. The below screenshot shows the result.
Output:
Conclusion
With the help of the above article, we saw Django include. From this article, we saw basic things about Django include, as well as the features and installation of Django include and how we use it in Django include.
Recommended Articles
We hope that this EDUCBA information on “Django Include” was beneficial to you. You can view EDUCBA’s recommended articles for more information.