LoginSignup
41
46

More than 1 year has passed since last update.

ワイ「Pythonの勉強したいなァ・・・Djangoってのがあるんか」[第一話]

Last updated at Posted at 2022-08-05

環境情報

  • MacBook Air (Monterey)
  • Docker Compose version v2.6.1

はじめに

当記事は続き物になっております。現在は2話まで更新していますのでもしよければ続けてお読み頂けますと幸いです。

導入

ワイ「なんや最近Next.jsしか触ってへんし別の言語使いたいなぁ」

ワイ「せや!昔Bot作ったことあるしPython触ったろ!」

ワイ「ほ〜ん、Djangoってのを使ってwebアプリも作れるんやな。ほなやってみるか」

環境構築

ワイ「ローカルに環境構築するん嫌やしな〜docker使うか〜」

ワイ「お、ええのあるやん!」

クィックスタート: Compose と Django

ワイ「適当にディレクトリ作って、と。まずはDockerfileからやな」

Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

ワイ「んで、requirements.txtってのを作るんか」

ワイ「なんやこれ?」

???「package.jsonみたいなものやで」

ワイ「なるほどな」

requirements.txt
Django>=1.8,<2.0
psycopg2

最後にdocker-compose.ymlやな

docker-compose.yml
version: '3'

services:
  db:
    image: postgres
    ports:
      - "5432"
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

ワイ「dbはpostgresか」

ワイ「ほな起動してみるか」

ワイ「え〜、コマンドはこれか」

docker-compose run web django-admin.py startproject composeexample .
Traceback (most recent call last):
  File "/usr/local/bin/django-admin.py", line 2, in <module>
    from django.core import management
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 13, in <module>
    from django.core.management.base import (
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 17, in <module>
    from django.db.migrations.exceptions import MigrationSchemaMissing
  File "/usr/local/lib/python3.10/site-packages/django/db/migrations/__init__.py", line 2, in <module>
    from .operations import *  # NOQA
  File "/usr/local/lib/python3.10/site-packages/django/db/migrations/operations/__init__.py", line 1, in <module>
    from .fields import AddField, AlterField, RemoveField, RenameField
  File "/usr/local/lib/python3.10/site-packages/django/db/migrations/operations/fields.py", line 4, in <module>
    from django.db.models.fields import NOT_PROVIDED
  File "/usr/local/lib/python3.10/site-packages/django/db/models/__init__.py", line 5, in <module>
    from django.db.models.deletion import (
  File "/usr/local/lib/python3.10/site-packages/django/db/models/deletion.py", line 5, in <module>
    from django.db.models import signals, sql
  File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/__init__.py", line 2, in <module>
    from django.db.models.sql.query import *  # NOQA
  File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 11, in <module>
    from collections import Counter, Iterator, Mapping, OrderedDict
ImportError: cannot import name 'Iterator' from 'collections' (/usr/local/lib/python3.10/collections/__init__.py)

ワイ「ん?エラー吐いたな」

ワイ「なになに、importが出来んってか?」

ImportError: cannot import name 'Iterable' from 'collections' in Python

ワイ「ほーん、python3.10でなんや削除されたんか」

ワイ「せやったらPythonのバージョン下げるか」

Dockerfile
FROM python:3.9-buster // ここを変更
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD ./requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

ワイ「ほんで、ビルドし直して再チャレンジや!」

docker-compose build
docker-compose run web django-admin.py startproject composeexample .
ls -l

total 32
-rw-r--r--  1 user  staff  155  8  5 12:17 Dockerfile
drwxr-xr-x  6 user  staff  192  8  5 12:19 composeexample/
-rw-r--r--  1 user  staff  211  8  5 11:45 docker-compose.yml
-rwxr-xr-x  1 user  staff  812  8  5 12:19 manage.py*
-rw-r--r--  1 user  staff   26  8  5 11:46 requirements.txt

ワイ「お〜、ちゃんと成功したみたいやな」

db接続設定

ワイ「次はDBの設定を書くみたいやな」

vi composeexample/settings.py

// 編集画面に移行後

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

ワイ「これでええんやな」

起動編

ワイ「ほな起動してみるか」

docker-compose up
[+] Running 2/2
 ⠿ Container django_cardlayout-db-1   Created                                                                                                                                                          0.0s
 ⠿ Container django_cardlayout-web-1  Recreated                                                                                                                                                        0.2s
Attaching to django_cardlayout-db-1, django_cardlayout-web-1
django_cardlayout-db-1   |
django_cardlayout-db-1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
django_cardlayout-db-1   |
django_cardlayout-db-1   | 2022-08-05 03:35:16.376 UTC [1] LOG:  starting PostgreSQL 14.4 (Debian 14.4-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
django_cardlayout-db-1   | 2022-08-05 03:35:16.386 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
django_cardlayout-db-1   | 2022-08-05 03:35:16.386 UTC [1] LOG:  listening on IPv6 address "::", port 5432
django_cardlayout-db-1   | 2022-08-05 03:35:16.390 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
django_cardlayout-db-1   | 2022-08-05 03:35:16.399 UTC [27] LOG:  database system was shut down at 2022-08-05 03:34:24 UTC
django_cardlayout-db-1   | 2022-08-05 03:35:16.409 UTC [1] LOG:  database system is ready to accept connections
django_cardlayout-web-1  | Performing system checks...
django_cardlayout-web-1  |
django_cardlayout-web-1  | System check identified no issues (0 silenced).
django_cardlayout-web-1  |
django_cardlayout-web-1  | You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
django_cardlayout-web-1  | Run 'python manage.py migrate' to apply them.
django_cardlayout-web-1  | August 05, 2022 - 03:35:17
django_cardlayout-web-1  | Django version 1.11.29, using settings 'composeexample.settings'
django_cardlayout-web-1  | Starting development server at http://0.0.0.0:8000/
django_cardlayout-web-1  | Quit the server with CONTROL-C.
django_cardlayout-web-1  | [05/Aug/2022 03:35:24] "GET / HTTP/1.1" 200 1716
django_cardlayout-web-1  | Not Found: /favicon.ico
django_cardlayout-web-1  | [05/Aug/2022 03:35:24] "GET /favicon.ico HTTP/1.1" 404 1970

ワイ「ええかんじやん」

ワイ「8000番にアクセスしてみるか」

ワイ「ブラウザ開いて localhost:8000 と」

スクリーンショット 2022-08-05 12.38.29.png

ワイ「ええやん」

ワイ「終了するのはCtrl + Cやな」

最後に

拙い記事ではありましたがここまで読み進めて頂きありがとうございました。

自分自身が後々読み返す際に読み飽きないようにこのようなスタイルになりました。逆に読みづらかった皆様へ、申し訳ありませんでした。

本記事は環境構築から起動確認までを行いました。次回はもう少し踏み込んでいく予定です。

また、自身の学習と並行して記事を作成している為不定期更新です。もっとも読んでくださる方がいるかは不明ですが。

ではまた次回。

41
46
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
46