[PYTHON] Django Tutorial (Blog App Creation) ① --Preparation, Top Page Creation

This time, we'll use Django to create a blog app that allows you to post articles.

I will introduce it in series, but first I will create it locally and then configure it with Docker, deploy it on AWS, and make a series of automatic test and deployment with CircleCI.

What is Django?

Django is a web application framework that you can implement in Python.

A framework is software that is a collection of functions used when developing an application. By introducing the framework, you can proceed with the web application efficiently.

This time we will create a blog application, but with Django you can easily create high-quality web applications such as content management systems and wikis, social networks, news sites, etc. using databases with a small amount of code. A simple web application can be created in minutes. Of course, you can extend the functionality to create complex web applications.

Django is also used in well-known web apps such as Instagram, and has become a notable framework equivalent to Ruby on Rails in Ruby.


First, create a working directory. The name here can be anything, but for now I'll leave it as a blog.

mkdir blog
cd blog

Installation of pipenv

If you do not pollute the base environment, you can eliminate the influence of other modules, so let's build a virtual environment using pipenv.

pip install pipenv
#Depending on the environment, you may not be able to use pip directly, so python3-Install with m pip install pipenv

After installation, execute the following command in the working folder.

pipenv shell

After execution, when you can enter the virtual environment, the character string according to the directory name will be displayed at the beginning of the command line.


Install Django

You can also run pipenv install django directly to install Django, Considering using Docker later, create a file called requirements.txt directly under the working directory. I will describe the required modules.

Right now I only need Django, so I'll write it along with the version information as follows:



Module installation

Install the module based on requirements.txt by executing the following command. Only Django should be installed here.

pipenv install -r requirements.txt

Create a Django project

Run directly under the blog directory

django-admin startproject mysite

I think that the file structure directly under the parent project looks like this.

├── Pipfile
├── Pipfile.lock
├── mysite
│   ├── manage.py
│   └── mysite
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
└── requirements.txt

Test launch of Django server

It has a function to start the development server. (If you've done Ruby on Rails, rails -s should be easy to understand.)

To do this, it is more convenient to go to the directory where the manage.py file is located, so go to the mysite / mysite directory before running it.

cd mysite
python3 manage.py runserver

If it can be executed normally, the following output will be output.

October 16, 2020 - 21:30:23
Django version 3.1, using settings 'mysite.settings'
Starting development server at
Quit the server with CONTROL-C.

In addition, this part is a message that is output because the integration process to the database called migrate has not been completed, but for now it is okay to not worry about it.

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

Now that Django's development server has started, check it from your browser. Type in the address bar of a browser such as Chrome and press Enter.


This is the beginning of everything. If you see this screen, the first step is complete! Congratulations.

Project settings

By the way, when you accessed the test server earlier, it was written in English. These settings can be set in mysite / settings.py, and by default the language display and time zone are adapted to the English-speaking world.




I'll change this as follows.



TIME_ZONE = 'Asia/Tokyo'

If you access the test server again, you will see that it is written in Japanese. (Although it cannot be confirmed from this screen, the time zone is also set to Tokyo)


Create a Django app

I created a project with the start-project command earlier, but next I will create an application. It's easy to get confused, but the underlying project and the individual apps are different.

This is the explanation on the official page.

What is the difference between a project and an app? An app is a web application that does something, such as a weblog system, a database of public records, or a small voting app. A project is a collection of specific website configurations and apps. A project can contain multiple apps. The app can exist in multiple projects.

Now let's create a blog app. Execute the following command directly under the mysite project (where the manage.py file is located).

python3 manage.py startapp blog

The current directory structure looks like this.

├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── blog
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

You can see that various files are created under the blog.

Now we need to tell the project that this app was created.

There is a column called "INSTALLED_APPS" in mysite / setting.py, so let's tell you the existence of the blog app in it.


    'blog.apps.BlogConfig', #Add here

template, view, url settings

First, "change each template.

The template is the part that creates the look and is the equivalent of an html file.

Create a templates folder directly under the mysite project, a blog folder under it, and index.html under it.

├── blog
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── templates
    └── blog
        └── index.html ← Create here

The contents of index.html are appropriate for the time being.


<h1>Hello, from Django!</h1>

You also need to tell the project where you created the templates folder. Just like when you set INSTALLED_APPS, put the following in settings.py.


        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')], #Fix here
        'APP_DIRS': True,

Next, modify views.py. Call index.html, the template you just created.


from django.views.generic import TemplateView

class IndexView(TemplateView):
    template_name = 'blog/index.html'

URL design

Next, create a routing setting specifically for the blog app. Routing settings are set in a file called "urls.py".

First of all, set the routing in urls.py, which controls the routing of the entire project, and urls.py in the app.

First, edit from urls.py directly under mysite.


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

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

You will load the urls for the blog app that you will create later in the urlpatterns.

In, urls.py is created only directly under mysite, but you can also create "urls.py" directly under blog. You can use an editor, or you can execute the following command in the blog directory of the app.


touch urls.py

The file structure under blog is like this.

├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
├── urls.py
└── views.py

By changing the contents of the created urls.py in this way, you can set the routing of the function (= index.html call process) created in views.py earlier.


from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),

By the way, by setting name ='index', you can call this url by reverse lookup using the name "blog: index".

At this point, check if index.html can be called.

Run runserver in the directory where manage.py is located.

python3 manage.py runserver

If you pass successfully, access ** ** with your browser. This is because when the address with blog is accessed in mysite / urls.py earlier, the contents described in the blog application will work.

If you access it and the contents of index.html are displayed, it is successful.


Next time, we will create models and prepare to actually register the article.

→ Continued: Django tutorial (blog application creation) ② --model creation, management site preparation

Recommended Posts

Django Tutorial (Blog App Creation) ① --Preparation, Top Page Creation
Django Tutorial (Blog App Creation) ⑤ --Article Creation Function
Django Tutorial (Blog App Creation) ④ --Unit Test
Django Tutorial (Blog App Creation) ③ --Article List Display
Django Tutorial (Blog App Creation) ⑦ --Front End Complete
Django tutorial (blog application creation) ② --model creation, management site preparation
Django Tutorial (Blog App Creation) ⑥ --Article Details / Editing / Deleting Functions
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Web App Development Practice: Create a Shift Creation Page with Django! (Introduction)
Django tutorial summary for beginners by beginners ① (project creation ~)
Web App Development Practice: Create a Shift Creation Page with Django! (Write a base template)
Web App Development Practice: Create a Shift Creation Page with Django! (Authentication system processing)
Python Django Tutorial (5)
Python Django Tutorial (2)
django table creation
django tutorial memo
Python Django Tutorial (8)
Python Django Tutorial (6)
Start Django Tutorial 1
Web App Development Practice: Create a Shift Creation Page with Django! (Experiment on admin page)
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
Python Django Tutorial (4)
What is a dog? Django App Creation Start Volume--startapp
Create a Todo app with Django ③ Create a task list page
What is a dog? Django App Creation Start Volume--startproject
Django page released on heroku: Preparation my addictive point
[Django] Added new question creation function to polls app
Web App Development Practice: Create a Shift Creation Page with Django! (Design of database model)