π Day 2: URLs, Views & Templates β Beginner's Web Dev Series


Welcome back to Day 2 of our Django for Beginners Series!
In Day 1, we set up our first Django project and app. Now itβs time to dive deeper into how Django connects URLs to views, serves HTML templates, and loads CSS and images using the static files system.
Letβs level up your project structure and presentation!
π§ What Weβll Cover Today
URL routing (project-level & app-level)
Function-based views
Using Django templates (HTML)
Configuring static files (CSS, images, js)
Dynamic URLs with path parameters
Final hands-on task
π 1. Detailed URL Routing in Django
In Django, URLs route the user to a specific view.
Folder structure:
βββ db.sqlite3
βββ homepage
β βββ __init__.py
β βββ admin.py
β βββ apps.py
β βββ migrations
β β βββ __init__.py
β βββ models.py
β βββ tests.py
β βββ urls.py
β βββ views.py
βββ manage.py
βββ mywebsite
βββ __init__.py
βββ asgi.py
βββ settings.py
βββ urls.py
βββ wsgi.py
Project-level (mywebsite/
urls.py
):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('homepage.urls')),
]
App-level (homepage/
urls.py
):
Create urls.py
inside your app folder if it doesnβt exist.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
π 2. Understanding Function-Based Views
Views are Python functions that return HTTP responses.
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome to Day 2!")
def about(request):
return HttpResponse("This is the About Page")
Now, each URL maps to a specific view function.
πΌοΈ 3. Django Templates β Display HTML
Django uses templates to render HTML pages with dynamic content.
Folder structure:
homepage/
βββ templates/
βββ homepage/
βββ home.html
home.html
:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello from Django Template!</h1>
</body>
</html>
Update view to use a template:
from django.shortcuts import render
def home(request):
return render(request, 'homepage/home.html')
Update view to use a template with context data:
from django.shortcuts import render
def home(request):
context = {
"page_title": "Django Day 2",
"message": "Welcome to Day 2 of Django!"
}
return render(request, 'homepage/home.html', context)
home.html
<!DOCTYPE html>
<html>
<head>
<title>{{ page_title }}</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>
π¨ 4. Static File Setup (CSS, Images, JS)
1. Project structure:
static/
βββ css/
βββ style.css
βββ images/
βββ logo.png
βββ scripts/
βββ app.js
2. In settings.py
:
import os
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
3. In urls.py
:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Django will now look inside the
static/
folder during development.
4. In home.html
:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>{{ page_title }}</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<img src="{% static 'images/logo.png' %}" alt="Logo" />
<h1>{{ message }}</h1>
<script src="{% static 'scripts/app.js' %}"></script>
</body>
</html>
π 5. Dynamic URLs with Path Parameters
Dynamic URLs let you pass variables via the URL.
In urls.py
:
path('hello/<str:username>/', views.greet_user, name='greet_user')
In views.py
:
def greet_user(request, username):
return HttpResponse(f"Hello, {username}!")
Try this:
π Visit http://127.0.0.1:8000/hello/Sabin/
Youβll see: Hello, Sabin!
π οΈ Final Task for Day 2
π§ Build a small webpage with these features:
Home route (
/
) should render a template saying: "Welcome to Day 2 of Django!"Add a route
/hello/<name>/
that greets the user dynamically.Add a route
/profile
that displays user details with dynamic data.Style the page using an external CSS file from
static/css/style.css
.Display an image on the page using
static/images/
.
β Bonus:
- Try adding a
for
loop in your template to render a list of items.
π§ Whatβs Next?
In Day 3, weβll cover:
Template inheritance (base layout)
Template tags and filters
Reusable template structure
Building a mini portfolio website
Subscribe to my newsletter
Read articles from Shankar Lamichhane directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shankar Lamichhane
Shankar Lamichhane
Hi, Iβm Shankar β a Sr. Software Engineer specializing in Python, Django, and DevOps. I build scalable web applications, APIs, and cloud-native systems, with a focus on clean architecture and backend automation.