iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📚

How to Output Web Page Based Screens Using Python and Django

に公開
2026/02/18

Overview

I am doing pair programming development in an IT-related online salon, and I want to share this as a reminder as well.

Author's Status

I work as a frontend engineer.
In practice, most projects are JS-based, so my Python skills are at a complete beginner level.
From this experience, I found some parts challenging due to Django-specific rules, but I started to understand the structure and writing style of routes, loops, etc., through implementation.

Development Environment

  • Python 3.7.4
  • OS: MacOS ver 11.1 (Big Sur)
  • Editor: VS Code

References

Django Official Documentation
Writing your first Django app, part 1
Trying out Django's Template Features

Development Steps

Creating the Django Project and App

With Python installed, navigate to your development directory in the terminal and enter the commands. This time we use the project name "todo_site", so it looks like this:

django-admin startproject todo_site

Then, create the "todo_app" application inside the project with the following command:

python manage.py startapp todo_app

To make this app loadable in the project, add the app name to the list of installed apps in the settings file inside "todo_site":

todo_site/settings.py (excerpt)
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'todo_app',     # Added line
]

This completes the preliminary setup.

Implementing the Webview Structure

The two pages we are creating are:

  • Index page (index): Displays only text.
  • User list page (user_list): Loads external CSS and displays a list using loop processing.

First, define the path structure for each page by specifying the path names and templates as follows:

todo_site/todo_app/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('user_list/', views.user_list, name='user_list'),
]

Since views refers to functions defined in views.py, define them next as follows:

todo_site/todo_app/views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("hogehogehoge")

def user_list(request):
    # User list
    context = {
        "items" : [
            {
                "user_name" : "User 1",
                "talk" : "Talk content",
                "updatetime" : "12:00",
            },
            {
                "user_name" : "User 2",
                "talk" : "Talk content",
                "updatetime" : "12:00",
            },
            {
                "user_name" : "User 3",
                "talk" : "Talk content",
                "updatetime" : "12:00",
            },
        ]
    }
    
    return render(request, 'todo_app/user_list.html', context)

For the index page, we only need to display plain text, so we import HttpResponse. On the other hand, the user list page implements a specification to display a card-style list in a loop, so in this file we define the list content in the variable context and use a separate template file to render it on the view side.

Implementing the View Using Templates

Create the template file todo_app/user_list.html referenced in the render statement in views.py.

Due to Django's file referencing specifications, it is recommended to double the directory structure to account for creating multiple index.html files.

I personally found this point a bit odd, but following the rules, I implemented it as below:

todo_site/todo_app/templates/todo_app/user_list.html
{% load static %}

<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Hello, world!</title>
<link rel="stylesheet" href="{% static 'todo_app/css/user_list.css' %}">
</head>
<body>
    {% for item in items %}
        <div class="item">
            <div class="item-icon">icon</div>
            <div class="item-detail">
                <div class="item-detail-username">{{ item.user_name }}</div>
                <div class="item-detail-talk">{{ item.talk }}</div>
            </div>
            <div class="item-status">
                <div class="item-status-updatetime">{{ item.updatetime }}</div>
            </div>
        </div>
    {% endfor %}
</body>
</html>

This structure loads items defined in the context function inside views.py and outputs them using Python's loop processing.

Furthermore, the following two lines load the external CSS:

load static
<link rel="stylesheet" href="{% static 'todo_app/css/user_list.css' %}">

We have created a new directory "static" for external sources.

todo_site/todo_app/static/todo_app/css/user_list.css (excerpt)
.item {
    height: 60px;
    width: 250px;
    border: solid 1px #000;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0px 5px;
}

Then start the app by running the following command or from the VSCode menu:

python manage.py runserver

The final views looked like this:

  • Index page

Screenshot 2021-03-12 18.47.14.png

  • User list page

Screenshot 2021-03-12 18.47.27.png

Discussion