Set your Publisher ID in src/config/ads.ts to display live ads.
Views, URL Dispatcher & HTTP Responses
Mapping URLs to view functions, path converters, and query parameters
Django's URL dispatcher uses the urls.py routing table to match incoming browser request URLs to Python view functions or class-based views (CBVs) that execute the business logic.
Routing and View Handlers
Path converters: Capture URL parameters cleanly with types like <int:pk>, <slug:course_slug>, or <str:username>.
View functions: Accept an HttpRequest object as their first argument and must return an HttpResponse or call render().
get_object_or_404(): Automatically queries a model or raises an HTTP 404 Not Found exception if the record does not exist.
# views.py
from django.shortcuts import render, get_object_or_404
from .models import Course
def course_list(request):
courses = Course.objects.filter(is_published=True).order_by('-created_at')
return render(request, 'courses/list.html', {'courses': courses})
def course_detail(request, pk):
course = get_object_or_404(Course, pk=pk, is_published=True)
return render(request, 'courses/detail.html', {'course': course})
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.course_list, name='course_list'),
path('<int:pk>/', views.course_detail, name='course_detail'),
]Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which shortcut helper retrieves a model instance or automatically raises a 404 HTTP error if missing?