The following article demonstrates an Example of template loading in Django.

In Django, template loading is the process of locating and loading templates for use in rendering views. Here’s an example of this.

  1. At first, create the app.
python manage.py startapp myapp
  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 a template named “my_template.html”.

  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>
</body>
</html>

This is a simple HTML template that just displays the text “Hello, World!”.

  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 “render” function in your view automatically locates and loads the “my_template.html” template from your app’s “templates” directory, and passes it to the Django template engine for rendering.


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