Django

Create and Render a Basic HTML Template using render()

๐Ÿ“Œ Introduction

In Django, the render() function is used to:

  • Load an HTML template
  • Pass data to it
  • Return it as an HTTP response

๐Ÿ‘‰ It is the most commonly used function in Django views.


๐ŸŽฏ Program Statement

๐Ÿ‘‰ Create and render a basic HTML template using Django render() function.


โš™๏ธ Step 1: Create Project and App

django-admin startproject myproject
cd myproject
python manage.py startapp myapp

โš™๏ธ Step 2: Register App


๐Ÿ“ File: settings.py

๐Ÿ”น Path:

myproject/myproject/settings.py

๐Ÿ”น Code:

INSTALLED_APPS = [
...
'myapp',
]

โš™๏ธ Step 3: Configure Templates Folder


๐Ÿ“ File: settings.py

๐Ÿ”น Path:

myproject/myproject/settings.py

๐Ÿ”น Code:

import os

TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
},
]

โš™๏ธ Step 4: Create View using render()


๐Ÿ“ File: views.py

๐Ÿ”น Path:

myproject/myapp/views.py

๐Ÿ”น Code:

from django.shortcuts import render

def home(request):
return render(request, 'home.html')

๐Ÿง  Explanation

render(request, 'home.html')
  • Loads home.html
  • Returns it as response
  • No data passed here

โš™๏ธ Step 5: URL Mapping


๐Ÿ“ File: urls.py

๐Ÿ”น Path:

myproject/myproject/urls.py

๐Ÿ”น Code:

from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
]

โš™๏ธ Step 6: Create Template


๐Ÿ“ File: home.html

๐Ÿ”น Path:

myproject/templates/home.html

๐Ÿ”น Code:

<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>

<h1>Welcome to Django</h1>
<p>This is my first template rendered using render() function.</p>

</body>
</html>

โš™๏ธ Step 7: Run Server

python manage.py runserver

๐ŸŒ Step 8: Output

Open:

๐Ÿ‘‰ http://127.0.0.1:8000/


โœ… Output:

Welcome to Django
This is my first template rendered using render() function.

๐Ÿง  How It Works

  1. User opens /
  2. URL calls home() view
  3. View calls render()
  4. Django loads home.html
  5. HTML is displayed in browser

๐Ÿง  Syntax of render()

render(request, template_name, context)

Example:

return render(request, 'home.html', {'name': 'Kavita'})

โš ๏ธ Common Errors


โŒ Template not found

๐Ÿ‘‰ Check:

templates/home.html

โŒ App not registered

๐Ÿ‘‰ Add 'myapp' in INSTALLED_APPS


โŒ Wrong template path

๐Ÿ‘‰ Use:

render(request, 'home.html')

NOT:

render(request, '/templates/home.html')

๐Ÿงช Practice Questions

  1. Create about.html and render it
  2. Display heading and paragraph using template
  3. Render multiple templates (home/about/contact)
  4. Add navigation links between pages

๐ŸŽค Viva Questions & Answers


1. What is render() function in Django?

render() is used to combine a template with data and return it as an HTTP response. It simplifies template rendering.


2. What are the parameters of render()?

It takes request, template_name, and optional context. Context is used to pass data.


3. What is a template in Django?

A template is an HTML file used to display content dynamically. It supports Django template language.


4. Where are templates stored?

Templates are usually stored in a templates folder inside the project or app directory.


5. What is the difference between render() and HttpResponse()?

render() loads HTML templates, while HttpResponse() returns plain text or simple HTML directly.


6. Why is render() preferred?

It supports templates, dynamic content, and clean code. It separates logic from presentation.


7. Can we pass data using render()?

Yes, using context dictionary like {'name': 'Kavita'}.


8. What happens if template does not exist?

Django raises TemplateDoesNotExist error.


9. What is dynamic rendering?

Displaying content based on data passed from views. It changes output dynamically.


10. Is render() mandatory in Django?

No, but it is the most commonly used and recommended method for returning HTML responses.


๐Ÿ‘‰ Next Post: Create Base Template in Django
๐Ÿ‘‰ Back to List: Django Programs (60 Questions with Solutions)


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

You may also like...

Leave a Reply

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