🌐 Lesson 53: Django Views & Templates (Dynamic Web Pages)
1. Views ဆိုတာဘာလဲ?
မြန်မာ → Django View ဆိုတာ request ကို လက်ခံပြီး response ပြန်ပေးတဲ့ function (သို့) class ဖြစ်တယ်။
English → A Django View is a function or class that receives a request and returns a response.
2. Templates ဆိုတာဘာလဲ?
မြန်မာ → Template ဆိုတာ HTML file တစ်ခုဖြစ်ပြီး, Django view က data တွေကို ထည့်ပေးနိုင်တယ်။
English → A Template is an HTML file where Django can inject dynamic data.
3. အကျဉ်းချုပ်
✅ Views → request ကို လက်ခံပြီး response ပြန်ပေးတယ်
✅ Templates → HTML file ထဲမှာ dynamic data ထည့်နိုင်တယ်
✅ Template Language → loops, conditions, variables
✅ Static Files → CSS, JS, Images integrate လုပ်နိုင်တယ်
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