我:“我想要学习Python……有没有叫Django的东西?”【第一集】
环境信息
-
- MacBook Air (Monterey)
- Docker Compose version v2.6.1
首先
此篇文章是一篇连载作品。目前已更新至第2话,如果方便的话,希望您能继续阅读下去。
-
- 第一話・・・ここ
- 第二話・・・ワイ「Pythonの勉強したいなァ・・・やっぱ最初はTODOやな!」[第二話]
引领进入
我最近只接触Next.js,想用些其他的语言。
我:“是的!我以前做过机器人,也碰过Python!”
我: “噢 ~ ,原来可以使用 Django 来制作网络应用程序啊。那么我来试试看吧。”
搭建环境
我:“我不喜欢在本地环境搭建,要不要用docker呢?”
我:“哦,这个不错!”
快速上手:Compose 和 Django
我:“随便创建一个目录,然后从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 这样的东西啦。”
我了解了
Django>=1.8,<2.0
psycopg2
最后是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
我:数据库是用PostgreSQL吗?
我:“那就试着启动吧。”
我:“啊~,这是指令吗?”
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)
我:“嗯?出错了啊。”
我:”喂喂,不能导入吗?”
无法从Python的collections模块中导入名称为’Iterable’的内容
我:”哦,Python 3.10 删除了什么东西吗?”
我: “要不然我们降低一下Python的版本怎么样?”
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
我:哇,看起来成功了呢。
数据库连接设置
我:“接下来好像是要写数据库的设置。”
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。”
我:好啊。
我: “结束的是按Ctrl + C”
在最后
非常感谢您阅读了我的拙劣文章直到这里。
为了让自己日后阅读时不感到乏味,我选择了这种风格。对于那些发现阅读困难的人,我感到非常抱歉。
本文涵盖了从环境设置到启动确认的过程。下次计划将进一步深入讨论。
此外,由于与自己的学习同时进行,我在撰写文章方面更新较不定期。虽然不确定是否有人愿意阅读,但还是希望能有人读。
下次再见。