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.
Those who want to get started with django
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
--Folder creation page
--Task creation page
--Task edit page
--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!
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!
$ docker-compose run web python3 manage.py migrate
When this command is executed, the migration will be performed as follows.
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')
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
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