$ source ~/django/bin/activate
新規プロジェクト作成
$ django-admin startproject TestProject
すると、ディレクトリ「TestProject」が作られた
$ ls -la TestProject
合計 8
drwxr-xr-x ? 3 his his ? 35 12月 22 14:02 .
drwx------. 22 his his 4096 12月 22 14:02 ..
-rwxr-xr-x ? 1 his his ?660 12月 22 14:02 manage.py
drwxr-xr-x ? 2 his his ? 89 12月 22 14:02 TestProject
更に、ディレクトリ「TestProject」の中の「TestProject」は
$ ls -la TestProject/TestProject
合計 16
drwxr-xr-x 2 his his ? 89 12月 22 14:02 .
drwxr-xr-x 3 his his ? 35 12月 22 14:02 ..
-rw-r--r-- 1 his his ? ?0 12月 22 14:02 __init__.py
-rw-r--r-- 1 his his ?385 12月 22 14:02 asgi.py
-rw-r--r-- 1 his his 3215 12月 22 14:02 settings.py
-rw-r--r-- 1 his his ?746 12月 22 14:02 urls.py
-rw-r--r-- 1 his his ?385 12月 22 14:02 wsgi.py
「settings.py」を編集して環境を構成
デフォルトでは他のPCからアクセス不可の様子なので
「ALLOWED_HOSTS = []」を「ALLOWED_HOSTS = ['*']」
と変更
デフォルトのデータベースがSqlite3なのでPostgreSQLへ変更
変更前
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
変更後
DATABASES = {
'default': {
##'ENGINE': 'django.db.backends.sqlite3',
##'NAME': BASE_DIR / 'db.sqlite3',
##'ENGINE': 'django.db.backends.postgresql_psycopg2', #どちらが正解?
'ENGINE': 'django.db.backends.postgresql', #どちらが正解?
'NAME': 'DB名',
'USER': 'UID',
'PASSWORD': 'P/W',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
それと
「LANGUAGE_CODE = 'en-us'」を「LANGUAGE_CODE = 'ja-jp'」
「TIME_ZONE = 'UTC'」を「TIME_ZONE = 'Asia/Tokyo'」
DB「dbtest」を作成して
$ cd TestProject
$ python manage.py migrate
DB「dbtest」内にテーブルと連番制御用のシークエンスが作成された
スーパーユーザーを作成?
$ python manage.py createsuperuser
Username (leave blank to use 'UID'):
Email address:
Password:
Password (again):
Superuser created successfully.
現在作業中のOSのユーザーで作成された様子
ポート開放
$ sudo firewall-cmd --zone=public --add-port=8000/tcp
$ sudo firewall-cmd --zone=public --add-port=8000/tcp --permanent
$ sudo firewall-cmd --reload
サーバー起動?
$ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
December 22, 2022 - 05:59:31
Django version 4.1.4, using settings 'TestProject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
で、他のPCでブラウザを起動して「http://サーバーIPアドレス:8000」
「Django」のサイトが表示されるのでヨシ?
で、「http://サーバーIPアドレス:8000/admin」から管理画面表示
ココまで
CTRL+C
$ deactivate