Web Development

Django Tutorial: Building Your First Polls App

Namraj Pudasaini

May 13, 2026

Django is a powerful Python web framework that makes it easy to build robust web applications quickly. In this tutorial, we will walk through creating a simple Polls application from scratch.

Prerequisites

Make sure you have Python installed and a virtual environment set up:

python3 -m django --version
mkvirtualenv first_django_app
workon first_django_app

Creating a Django Project

First, create a new Django project:

mkdir djangotutorial
django-admin startproject mysite djangotutorial

This creates a mysite project inside the djangotutorial directory with the basic project structure.

Starting the Development Server

Run the server to make sure everything is working:

python3 manage.py runserver

Visit http://127.0.0.1:8000/ in your browser to see the default Django welcome page.

You will probably also see a warning in the console:

You have N unapplied migration(s). Your project may not work properly until you
apply the migrations for app(s): admin, auth, contenttypes, sessions.

That is expected on a fresh project. Django ships with built-in apps that need database tables, and you have not created them yet. The migrate command further down takes care of it, so you can ignore the warning until then.

Creating the Polls App

Django projects are made up of apps. Each app handles a specific feature. Let's create the Polls app:

python3 manage.py startapp polls
python3 manage.py runserver

Writing Your First View

Open polls/views.py and add a simple view:

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

Next, create a polls/urls.py file to map a URL to this view:

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
]

Now wire it up in the project's root mysite/urls.py:

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

urlpatterns = [
    path("polls/", include("polls.urls")),
    path("admin/", admin.site.urls),
]

Visit http://127.0.0.1:8000/polls/ and you should see "Hello, world. You're at the polls index."

Setting Up the Database

Django uses a powerful ORM to manage your database. Run the initial migration to set up the default database:

python3 manage.py migrate

Add your app to INSTALLED_APPS in mysite/settings.py:

INSTALLED_APPS = [
    "polls.apps.PollsConfig",
    # ... other apps
]

Then create and apply migrations for the Polls app:

python3 manage.py makemigrations polls
python3 manage.py sqlmigrate polls 0001
python3 manage.py migrate

The sqlmigrate command shows you the SQL that Django will execute — a great way to understand what is happening under the hood.

Using the Django Shell

Django provides an interactive shell for testing your models and queries:

python3 manage.py shell

This is useful for quick experiments without writing full views or tests.

Introducing the Django Admin

Django comes with a built-in admin interface. Create an admin user:

python3 manage.py createsuperuser

Follow the prompts to set a username, email, and password. Then start the server and visit http://127.0.0.1:8000/admin/ to log in.

Summary

In this tutorial, you learned how to:

  • Create a Django project and app
  • Write a simple view and configure URL routing
  • Set up database migrations
  • Use the Django shell
  • Create an admin user and access the admin interface

This is the foundation for building more complex Django applications. From here, you can add models, forms, templates, and more to your Polls app.

Related Posts