インフラエンジニアは人間じゃない

インターネットの闇、炎上ネタ、迷惑メールや詐欺、あらゆる危険な情報を扱うブログです。

MENU

Pythonド初心者がPythonでWebアプリケーションを作ろうとしてみた結果wwwww

 

 

 

pip install django

 

C:\temp\05-Fortune_root>pip install django
Collecting django
Downloading https://files.pythonhosted.org/packages/36/50/078a42b4e9bedb94efd3e0278c0eb71650ed9672cdc91bd5542953bec17f/Django-2.1.5-py3-none-any.whl (7.3MB)
100% |████████████████████████████████| 7.3MB 2.7MB/s
Requirement already satisfied: pytz in c:\users\yst15\appdata\local\programs\python\python37\lib\site-packages (from django) (2018.7)
Installing collected packages: django
Successfully installed django-2.1.5

C:\temp\05-Fortune_root>

 

pip freeze

C:\temp\05-Fortune_root>pip freeze
certifi==2018.11.29
chardet==3.0.4
Click==7.0
cycler==0.10.0
DateTime==4.3
Django==2.1.5
hello52==0.0.5
idna==2.8
kiwisolver==1.0.1
matplotlib==3.0.2
numpy==1.15.4
pep8==1.7.1
py2exe==0.9.2.2
pyparsing==2.3.0
python-dateutil==2.7.5
pytz==2018.7
requests==2.21.0
scipy==1.2.0
six==1.12.0
tqdm==4.28.1
urllib3==1.24.1
zope.interface==4.6.0

 

C:\temp\05-Fortune_root>django-admin.py startproject Fortune

 

C:\temp\05-Fortune_root>dir
ドライブ C のボリューム ラベルがありません。
ボリューム シリアル番号は 28C3-9547 です

C:\temp\05-Fortune_root のディレクト

2019/01/15 21:13 <DIR> .
2019/01/15 21:13 <DIR> ..
2019/01/15 21:13 <DIR> Fortune
2019/01/15 21:00 38 index.html
2019/01/15 21:13 0 pip
2019/01/15 21:01 237 simpleserver.py
3 個のファイル 275 バイト
3 個のディレクトリ 467,491,643,392 バイトの空き領域

 

c:\temp\05-Fortune_root>python Fortune\manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 15 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.
January 15, 2019 - 21:15:34
Django version 2.1.5, using settings 'Fortune.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

 

 

http://127.0.0.1:8000/

 

f:id:nell17world:20190115211622p:plain

django デフォルト画面

 

 

 

Python言語コードを日本語に設定する

C:\temp\05-Fortune_root\Fortune\Fortune\ettings.pyをテキストファイルで開く

LANGUAGE_CODE = 'ja'に変更する

TIME_ZONE = 'Asia/Tokyo'に変更する

 

c:\temp\05-Fortune_root>python Fortune\manage.py startapp Test1

 

c:\temp\05-Fortune_root>dir
ドライブ C のボリューム ラベルがありません。
ボリューム シリアル番号は 28C3-9547 です

c:\temp\05-Fortune_root のディレクト

2019/01/15 21:20 <DIR> .
2019/01/15 21:20 <DIR> ..
2019/01/15 21:14 <DIR> Fortune
2019/01/15 21:00 38 index.html
2019/01/15 21:13 0 pip
2019/01/15 21:01 237 simpleserver.py
2019/01/15 21:20 <DIR> Test1
3 個のファイル 275 バイト
4 個のディレクトリ 467,489,325,056 バイトの空き領域

 

settings.pyへapp Test1 を追加

 

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

 

 

 

仕切り直し

-------------------

 django-admin startproject project_name

 

manage.pyのあるディレクトリで

python manage.py startapp app_name

 

project_name\project_name\settings.py をテキストエディタで編集

 app_nameのエントリを追加

 

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

 

C:\temp\05-Fortune_root\project_name\app_name

models.pyにデータを定義(サンプルをコピー)

 

 

 

 

python manage.py makemigrations
python manage.py migrate

 

エラーが発生

TypeError: __init__() missing 1 required positional argument: 'on_delete'

 

 

Foreign keyを使う場合、on_deleteの引数が必須

models.py内のforgeinにon_delete(デリート時の動作)を追記した

 

c:\temp\05-Fortune_root\project_name>python manage.py makemigrations
Migrations for 'app_name':
app_name\migrations\0001_initial.py
- Create model Manager
- Create model Person
- Create model Worker
- Add field person to manager

c:\temp\05-Fortune_root\project_name>python manage.py migrate
Operations to perform:
Apply all migrations: admin, app_name, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying app_name.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK

c:\temp\05-Fortune_root\project_name>

 

 

ここで力尽きました…次回をお楽しみに…!