0Pricing
Python Academy · Lesson

Views and URLs

Route requests to views.

Views and URLs is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a View?

A Django view is a function (or class) that takes a web request and returns a web response. Views hold your page logic and live in an app's views.py.

from django.http import HttpResponse

def home(request):
    return HttpResponse('Hello, world!')
print('A view takes request, returns response')

The request Argument

Every view receives a request object holding details about the incoming request: method, GET/POST data, user, and more.

from django.http import HttpResponse

def info(request):
    return HttpResponse('Method: ' + request.method)
print('request carries the incoming data')

URL Patterns

An app's urls.py maps URL paths to views using a list called urlpatterns and the path() function.

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

urlpatterns = [
    path('', views.home),
]
print('path maps a URL to a view')

Naming URLs

Give a pattern a name so you can refer to it elsewhere without hard-coding the path.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]
print("name='home' lets you reference it later")

URL Parameters

Capture parts of the URL with converters like <int:post_id>. The captured value is passed to the view.

from django.urls import path
from . import views

urlpatterns = [
    path('post/<int:post_id>/', views.detail, name='detail'),
]
print('post/5/ -> detail(request, post_id=5)')

Reading the Parameter

The captured value arrives as a keyword argument matching the converter's name.

from django.http import HttpResponse

def detail(request, post_id):
    return HttpResponse('Post ' + str(post_id))
print('View receives post_id')

Including App URLs

The project's root urls.py pulls in each app's urls with include(), keeping routing modular.

# mysite/urls.py
from django.urls import path, include

urlpatterns = [
    path('blog/', include('blog.urls')),
]
print('include() wires up app urls')

Rendering Templates

The render() shortcut combines a template with a context dict and returns an HttpResponse.

from django.shortcuts import render

def home(request):
    return render(request, 'home.html', {'name': 'Alice'})
print('render combines template + context')

404 Helpers

get_object_or_404() fetches an object or raises a 404 if it is missing, a common and safe pattern.

from django.shortcuts import get_object_or_404, render
from .models import Post

def detail(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    return render(request, 'detail.html', {'post': post})
print('get_object_or_404 handles missing data')

Reversing URLs

In Python, reverse('detail', args=[5]) builds a URL from a pattern name. In templates, the {% url %} tag does the same.

from django.urls import reverse
# reverse('detail', args=[5]) -> '/blog/post/5/'
# In a template: {% url 'detail' post.id %}
print('reverse builds URLs from names')

Class-Based Views

Django also offers class-based views like ListView and DetailView that handle common patterns with less code.

from django.views.generic import ListView
from .models import Post

class PostList(ListView):
    model = Post
print('ListView lists objects automatically')

Quick Check

Test your views and URLs knowledge.

Recap

You routed requests to views.

  • Views take a request and return a response
  • urlpatterns + path() map URLs to views, optionally with a name
  • Converters like <int:id> capture URL parts
  • include() wires app urls; render() and get_object_or_404() are handy shortcuts

Frequently asked questions

Is the “Views and URLs” lesson free?

Yes — the full text of “Views and URLs” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Views and URLs”?

Route requests to views. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Views and URLs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Projects and Apps
  2. Models and Migrations
  3. Views and URLs
  4. The Django Admin
← Back to Python Academy