← All tutorials

Getting started / 5 MIN READ

Your first Django view and URL

Follow a request from a URL to a Python function and return your first response.

01 Start with a project

Use the project from the installation tutorial. Make sure core appears in INSTALLED_APPS. A view receives an HTTP request and returns an HTTP response.

02 Write a view

Replace the contents of core/views.py with a small function. HttpResponse is useful when you only need to return a short piece of text.

Code example
from django.http import HttpResponse


def home(request):
    return HttpResponse("Hello, Django!")

03 Connect the URL

In config/urls.py, connect the empty path to home. The empty string represents the homepage, and the name lets other parts of the application refer to this route without hardcoding its address.

Code example
from django.contrib import admin
from django.urls import path
from core.views import home

urlpatterns = [
    path("", home, name="home"),
    path("admin/", admin.site.urls),
]

04 Try the request

Start the development server and open the homepage. You should see Hello, Django! Change the message and refresh the browser to see your edit. Next, move the HTML into a template.

Code example
python manage.py runserver
# In another terminal:
curl http://127.0.0.1:8000/
Keep exploring

Read the official Django documentation for details and more examples.