The following article demonstrates an Example of include template tag in Django.

In Django, the “include” template tag is used to include the contents of another template within the current template. Here’s an example of how to use the “include” tag in Django.

  1. Create a new Django app inside your project.
python manage.py startapp myapp
  1. Create a new file called “my_include.html” inside your app’s “templates” directory, and add the following code.
<h1>This is my included template</h1>
<p>It can be used in other templates using the include tag</p>

This is a simple HTML template that will be included in another template using the “include” tag.

  1. Create a new file called “my_template.html” inside your app’s “templates” directory, and add the following code.
<!DOCTYPE html>
<html>
<head>
    <title>My Template</title>
</head>
<body>
    <h1>Hello, World!</h1>
    {% include 'my_include.html' %}
</body>
</html>

This is a simple HTML template that includes the “my_include.html” template using the “include” tag. The “include” tag takes a single argument, which is the name of the template to include. In this case, we’re including the “my_include.html” template.

  1. Open the “views.py” file inside your app’s directory, and add the following code.
from django.shortcuts import render

def my_view(request):
    return render(request, 'my_template.html')

This defines a new view named “my_view” that simply renders the “my_template.html” template.

  1. Finally, add a URL pattern for your new view in your project’s “urls.py” file.
from django.urls import path
from myapp.views import my_view

urlpatterns = [
    path('my-url/', my_view, name='my_url'),
]

This maps the URL “/my-url/” to your new view.

Now you can run your Django development server and visit the URL “/my-url/” to see your template in action! The “my_template.html” template includes the contents of the “my_include.html” template using the “include” tag, so you should see both the “Hello, World!” heading and the contents of the “my_include.html” template displayed on the page.


Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com