Create a Todo app with Django ① Build an environment with Docker

Introduction

I made a Todo app that is a representative of the tutorial with Django. I intend to write it in as much detail as possible in terms of the meaning of the code.

Target person

Those who want to get started with django

List of articles

Create a Todo app with Django ① Build an environment with Docker Create a Todo app with Django ② Create a folder list page Create a Todo app with Django ③ Create a task list page Create Todo app with Django ④ Implementation of folder and task creation function Create a Todo app with Django ⑤ Create a task editing function

Completion drawing

--Folder creation page フォルダー作成機能.png

--Task creation page タスク作成機能.png

--Task edit page タスク編集ページ.png

List of Functions

--Task creation function --Task editing function --Task list display --Folder creation function --Folder list display

It looks like this when the functions are roughly classified! Let's make it!

Build development environment with Docker

Create the following directory structure.

django_todo
├── docker-compose.yml
├── dockerfile
├── manage.py
├── requirements.txt
└── django_todo
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-38.pyc
    │   ├── settings.cpython-38.pyc
    │   ├── urls.cpython-38.pyc
    │   └── wsgi.cpython-38.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py

** 1. Creating a working directory **

$ mkdir django_todo
$ cd django_todo

** 2. Create a docker file, requirements.txt ** Create Dockerfile, docker-compose.yml and requirements.txt under the django_todo directory.

$ touch Dockerfile docker-compose.yml requirements.txt

3.Dockerfile Describe as follows in Dockerfile, docker-compose.yml, requirements.txt.

Dockerfile



#Specify the base image
FROM python:3
#Setting not to hold data in the buffer(Any character does not have to be 1)
ENV PYTHONUNBUFFERED 1
#Create a directory inside the container
RUN mkdir /code
#Specify working directory
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

Docker-compose.yml


#Specify yml file version
version: '3'
#Define container
services:
    web:
        #Build Dockerfile
        build: .
        command: python3 manage.py runserver 0.0.0.0:8000
        #Specifying the mount location
        volumes:
            - .:/code
        ports:
            - "8000:8000"
        #Specify to start the db service after it starts
        depends_on:
            - db
    db:
        image: postgres
        ports: 
            - "5432"
        environment:
          - POSTGRES_DB=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres

requirements.txt


Django>=2.0,<3.0
psycopg2>=2.7,<3.0

By describing requirements.txt, the described packages can be installed in a batch with the specified version.

** 4. Create a Django project **

$ docker-compose run web django-admin.py startproject django_todo .

This tells Compose to run django-admin.py startproject django_todo inside the container. As a result, the directory structure will be as follows.

django_todo
├── Dockerfile
├── django_todo
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── docker-compose.yml
├── manage.py
└── requirements.txt

** 5. Database settings ** The database uses postgresql. Edit django_todo / setting.py to set up the database. DATABASES = ... ALLOWED_HOSTS =... To the following.

setting.py


ALLOWED_HOSTS = ['localhost']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

** 6. Image construction, container construction / launch ** Use the following commands to build an image and build / start a container.

$ docker-compose up --build

** 7. Environment construction completed! ** ** http://localhost:8000/ If you access and the following screen is displayed, it's OK! Screenshot from 2020-03-16 17-36-49.png

Database setup

$ docker-compose run web python3 manage.py migrate

When this command is executed, the migration will be performed as follows.

Screenshot from 2020-04-16 23-17-22.png

Create a new application

Run the following command to create the application.

$ docker-compose run web python3 manage.py  startapp todo

The directory structure after execution is as follows.

django_todo
├── Dockerfile
├── django_todo
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── settings.cpython-38.pyc
│   │   ├── urls.cpython-38.pyc
│   │   └── wsgi.cpython-38.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── docker-compose.yml
├── manage.py
├── requirements.txt
└── todo
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

Once you've created your application, you need to tell Django to use it. To do this, add todo to INSTALLED_APPS in django_todo / settings.py. So settings.py looks like this:

setting.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'todo',
]

Also, set the time zone, language, and static file path here as follows.

setting.py


LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'

setting.py


STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

in conclusion

This is the end of environment construction! The code so far can be found in the repository chapter1 branch. In the next chapter, we will implement the folder list display! Create a Todo app with Django ② Create a folder list page

List of articles

Create a Todo app with Django ① Build an environment with Docker Create a Todo app with Django ② Create a folder list page Create a Todo app with Django ③ Create a task list page Create Todo app with Django ④ Implementation of folder and task creation function Create a Todo app with Django ⑤ Create a task editing function

Recommended Posts

Create a Todo app with Django ① Build an environment with Docker
Create a Todo app with Django REST Framework + Angular
Create a Todo app with the Django REST framework
Create a Todo app with Django ③ Create a task list page
Create a Todo app with Django ⑤ Create a task editing function
Build a development environment with Poetry Django Docker Pycharm
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
[Memo] Build a development environment for Django + Nuxt.js with Docker
[Django] Build a Django container (Docker) development environment quickly with PyCharm
[Linux] Build a jenkins environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build Django + NGINX + PostgreSQL development environment with Docker
Create a python3 build environment with Sublime Text3
Build a Django environment with Vagrant in 5 minutes
Create Nginx + uWSGI + Python (Django) environment with docker
Build a Django development environment with Doker Toolbox
Quickly build a Python Django environment with IntelliJ
Create a Todo app with Django ④ Implement folder and task creation functions
Create an environment with virtualenv
Create an API with Django
Create a homepage with django
How to build a Django (python) environment on docker
Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Build a Django environment for Win10 (with virtual space)
Build a go environment using Docker
Build a deb file with Docker
Build Mysql + Python environment with docker
Deploy a Django application with Docker
Create a virtual environment with Python!
Build PyPy execution environment with Docker
Build a bulletin board app from scratch with Django. (Part 2)
Build a web application with Django
Build a bulletin board app from scratch with Django. (Part 3)
Create a simple Python development environment with VSCode & Docker Desktop
Create a file uploader with Django
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Create an arbitrary machine learning environment with GCP + Docker + Jupyter Lab
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Easily build a development environment with Laragon
Build Jupyter Lab (Python) environment with Docker
Build a blockchain with Python ① Create a class
Build a Tensorflow environment with Raspberry Pi [2020]
[Python] Create a virtual environment with Anaconda
Create a GUI app with Python's Tkinter
Get a local DynamoDB environment with Docker
Create Python + uWSGI + Nginx environment with Docker
Create a virtual environment with Python_Mac version
Create a simple web app with flask
Create a Python-GUI app in Docker (PySimpleGUI)
Create your first app with Django startproject
Create an English word app with python
Build a python virtual environment with pyenv
Create a web service with Docker + Flask
Build a modern Python environment with Neovim
Build NGINX + NGINX Unit + MySQL environment with Docker