Prep Template and View

Creating your first Django template and view

🎯 What are Templates and Views?

Templates are HTML files that display data, while views are Python functions that process requests and return responses. Together, they create dynamic web pages in Django.


# views.py - Simple view function
from django.shortcuts import render

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

Understanding the Flow

🌐

Request

User visits a URL

http://localhost:8000/
⚙️

View

Processes the request

def home(request):
    return render(...)
📄

Template

HTML file with data

<h1>{{ title }}</h1>

Response

Rendered page sent back

HttpResponse

🔹 Step 1: Create a View

Views are Python functions in views.py that handle requests. They receive a request object and return a response. Let's create a simple view that renders a template with some context data.

# myapp/views.py
from django.shortcuts import render

def home(request):
    context = {
        'title': 'Welcome to Django',
        'message': 'Your first Django page!'
    }
    return render(request, 'home.html', context)

View Components:

  • request: Contains information about the HTTP request
  • context: Dictionary of data passed to template
  • render(): Combines template with context data

🔹 Step 2: Create Template Folder

Django looks for templates in specific locations. Create a templates folder inside your app directory to organize your HTML files properly.

# Create templates directory structure
myapp/
    templates/
        home.html
    views.py
    urls.py

Template Location:

Django searches for templates in the templates/ folder of each installed app.

🔹 Step 3: Create the Template

Templates use Django template language to display dynamic data. Use double curly braces to insert variables from the context dictionary into your HTML.

<!-- myapp/templates/home.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>{{ title }}</h1>
    <p>{{ message }}</p>
</body>
</html>

Output:

Welcome to Django

Your first Django page!

🔹 Step 4: Configure URLs

Connect your view to a URL pattern so Django knows which view to call when a user visits a specific URL path.

# myapp/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]
# project/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

🔹 Passing Multiple Variables

You can pass multiple variables to templates using the context dictionary. This allows you to display dynamic content throughout your page.

# views.py
def about(request):
    context = {
        'name': 'John Doe',
        'age': 25,
        'skills': ['Python', 'Django', 'HTML']
    }
    return render(request, 'about.html', context)
<!-- about.html -->
<h1>About {{ name }}</h1>
<p>Age: {{ age }}</p>
<ul>
    {% for skill in skills %}
        <li>{{ skill }}</li>
    {% endfor %}
</ul>

Output:

About John Doe

Age: 25

  • Python
  • Django
  • HTML

🧠 Test Your Knowledge

What function is used to render templates in Django?