Django·Django from project to API

Views and URLs

Map URLs to views and return real HTTP responses.

Intermediate 13 min read Lesson 2 of 300 Challenge included
What you will be able to do
  • Write function views for simple pages.
  • Use path converters such as <slug:slug>.
  • Return HttpResponse or render a template.
  • Include app URLs from the project urls.py.

In plain English

A view is a function or class that receives a request and returns a response. URL patterns connect paths to views. Keep routing in the app and include it from the project so features stay modular.

views.pyPython
from django.http import HttpResponse
from django.shortcuts import render

def home(request):
    return HttpResponse('Hello from Django')

def lesson(request, slug: str):
    return render(request, 'lessons/detail.html', {'slug': slug})
Your turn

Add a slug route

Easy

Add a URL pattern that captures a slug and passes it to a view.

PYTHON · starter
urlpatterns = []
Check your understanding before moving on.
Knowledge checkQuestion 1

What does a Django view return?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Templates
Lesson 2 of 300 · 0% of course