Hello World Example

Build a minimal ôHello, world!ö: write a function-based view, map a URL, and render a simple template to confirm your project is working end-to-end.

1. Introduction

In this guide we will build a simple Hello World page in Django. This means writing a view, connecting it to a URL, and seeing it in the browser. It is the shortest path to confirm that your entire setup works end to end.

  • Your myproject project must be set up. If not, start from Create First Project.
  • The pages app must be created and registered in INSTALLED_APPS. If not, see Create First App.
  • Your .venv must be active and Django 5.2 installed.

2. How Django handles a request

Before we write any code, it helps to understand what happens when someone visits a URL in a Django project:

  1. A request comes in for a URL, for example /hello/.
  2. Django checks mysite/urls.py to find a matching URL pattern.
  3. The matching pattern points to a view function or class.
  4. The view runs its logic and returns a response.
  5. Django sends that response back to the browser.

3. Write the view

Open pages/views.py. It currently contains one import line. Replace the entire file with this:

# pages/views.py

from django.http import HttpResponse


def hello_world(request):
    return HttpResponse("Hello, World!")

This is a function-based view. It takes a request object and returns an HttpResponse with the text "Hello, World!". That is all a view needs to do at its most basic level.

4. Create the app URL file

Django does not create a urls.py file inside app folders automatically. We create it ourselves. Inside the pages folder, create a new file called urls.py and add this:

# pages/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.hello_world, name='hello-world'),
]

Here is what each part does:

  • path('hello/', ...) — maps the URL /hello/ to our view.
  • views.hello_world — points to the function we just wrote in views.py.
  • name='hello-world' — gives this URL a name so we can reference it in templates and other views without hardcoding the URL.

5. Connect to the project URLs

Right now mysite/urls.py does not know about the pages app URLs. We need to include them. Open mysite/urls.py and update it:

# mysite/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('pages.urls')),
]

The include('pages.urls') tells Django to look inside pages/urls.py for any URL patterns when a request comes in. The empty string '' means the pages app URLs are available at the root level — so /hello/ works directly.

6. Test it in the browser

Start the development server if it is not already running:

python manage.py runserver

Open your browser and go to:

http://127.0.0.1:8000/hello/

You should see the text Hello, World! in plain text on the page. That confirms your view, URL, and project config are all working correctly together.

7. Your project structure now

After this tutorial your myproject folder looks like this:

myproject/
├── .venv/
├── manage.py
├── requirements.txt
├── mysite/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── asgi.py
│   └── wsgi.py
└── pages/
    ├── migrations/
    │   └── __init__.py
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── models.py
    ├── tests.py
    ├── urls.py        ← new file we created
    └── views.py      ← updated with our view

8. Next steps

You have built your first working Django page. Right now the view returns plain text. In the next section of these tutorials we will start using templates to return proper HTML pages and learn how Django's template system works.


Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.