Build a Simple Poll App with Django: Step‑by‑Step Guide

This tutorial walks you through installing Django, creating a project and a polls app, setting up the development server, writing views and URL configurations, and explains key concepts like include() and url() arguments, enabling you to build a functional poll website.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a Simple Poll App with Django: Step‑by‑Step Guide

The author translates an English Django tutorial to help readers overcome language barriers.

Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic poll application.

The tutorial covers two parts: a public site for viewing and voting on polls, and an admin site for managing polls.

Assume Django is installed; you can verify the installation and version with the following command:
If Django is not installed, install it via pip install Django . If the version does not match, update Django or adjust code for Python 2.7 as noted.

Creating a project

Run django-admin startproject mysite to generate the project structure.

Do not name the project after built‑in components such as django or test.

Place your code outside the web server’s document root (e.g., /home/mycode) for security.

The development server

Start the server with python manage.py runserver. It will be available at http://127.0.0.1:8000/ .

Ignore initial migration warnings; the server is for development only.

Change the port with python manage.py runserver 8080 or bind to all IPs with python manage.py runserver 0.0.0.0:8000 .

Creating the Polls app

Create the app with python manage.py startapp polls. The resulting polls directory contains the basic structure for the app.

Write your first view

In polls/views.py add a simple view that returns the text “Hello, world. You’re at the polls index.”

Create polls/urls.py with a URL pattern for the view and include it in mysite/urls.py using include().

The include() function allows modular URL configurations, making it easy to plug‑and‑play URLs across different apps.

The url() function takes four arguments: regex (the pattern to match), view (the callable to invoke), optional kwargs , and optional name for reverse URL resolution.

When you’re comfortable with the basic request‑response flow, proceed to part 2 of the tutorial to work with the database.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPythonDjangoWeb DevelopmentTutorialPoll App
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.