Thuta Learning
IntermediateProgrammingbeginner

Django Views & Templates

Relax. We'll talk through this in plain words — no textbook voice.

🌐 Lesson 53: Django Views & Templates (Dynamic Web Pages)

1. What are Views?

In short → A Django View is a function (or class) that receives a request and returns a response.

English → A Django View is a function or class that receives a request and returns a response.

2. What are Templates?

In short → A Template is an HTML file that a Django view can inject data into.

English → A Template is an HTML file where Django can inject dynamic data.

3. Summary

✅ Views → receive a request and return a response

✅ Templates → can inject dynamic data into HTML files

✅ Template Language → loops, conditions, variables

✅ Static Files → integrate CSS, JS, and images

python
# ===== 1. Function-based View =====
print("===== views.py =====")
print("from django.http import HttpResponse")
print("from django.shortcuts import render")
print("")
print("def home(request):")
print("    return HttpResponse('Hello, this is Home Page!')")

# ===== 2. Template Rendering =====
print(f"\n===== Template View =====")
print("def home(request):")
print("    context = {'name': 'Sai'}")
print("    return render(request, 'home.html', context)")

# ===== 3. URL Configuration =====
print(f"\n===== urls.py =====")
print("from django.urls import path")
print("from myapp import views")
print("")
print("urlpatterns = [")
print("    path('', views.home, name='home'),")
print("]")

# ===== 4. Template Example =====
print(f"\n===== home.html Template =====")
print("<!DOCTYPE html>")
print("<html>")
print("<head><title>Django Template</title></head>")
print("<body>")
print("    <h1>Hello {{ name }}!</h1>")
print("    {% if name == 'Sai' %}")
print("        <p>Welcome!</p>")
print("    {% endif %}")
print("</body>")
print("</html>")

# ===== 5. Template Language Features =====
print(f"\n===== Template Language =====")
print("# Variables: {{ variable }}")
print("# Tags: {% tag %}")
print("# Loops: {% for item in list %}")
print("# Conditions: {% if condition %}")
print("# Static files: {% load static %}")

# ===== 6. Static Files =====
print(f"\n===== Static Files =====")
print("# settings.py")
print("STATIC_URL = '/static/'")
print("")
print("# Template")
print("<link rel='stylesheet' href='{% static 'style.css' %}'>")

# ===== 7. Real-World Use Cases =====
print(f"\n===== Use Cases =====")
print("✅ Blog → Post list page, detail page")
print("✅ E-commerce → Product list, product detail")
print("✅ Dashboard → Dynamic charts, tables")
print("✅ Social media → Profile page, feed page")
You should see
===== views.py ===== from django.http import HttpResponse from django.shortcuts import render def home(request): return HttpResponse('Hello, this is Home Page!') ===== Template View ===== def home(request): context = {'name': 'Sai'} return render(request, 'home.html', context) ===== urls.py ===== from django.urls import path from myapp import views urlpatterns = [ path('', views.home, name='home'), ] ===== home.html Template ===== Django Template Hello {{ name }}! {% if name == 'Sai' %} Welcome! {% endif %} ===== Template Language ===== # Variables: {{ variable }} # Tags: {% tag %} # Loops: {% for item in list %} # Conditions: {% if condition %} # Static files: {% load static %} ===== Static Files ===== # settings.py STATIC_URL = '/static/' # Template ===== Use Cases ===== ✅ Blog → Post list page, detail page ✅ E-commerce → Product list, product detail ✅ Dashboard → Dynamic charts, tables ✅ Social media → Profile page, feed page
Django Views & Templates | Thuta Learning