ひこぽんのーと

覚書と雑記です。

Djangoをやってみよう。 その7-管理サイト-

その6のつづき。

Apache TomcatにWebアプリケーションを登録する時、
管理画面を使ってデプロイすることができる。
これと似たようなもので、Djangoにも管理画面がある。
管理画面を使うには管理者ユーザを作成する必要がある。

と、その前に以下のコマンドを実行して管理画面等、基本機能を使用可能状態にする。
管理テーブルなんかが作られるため、プロジェクト作成後にやってしまえばよかった。

 python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, warship
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 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 sessions.0001_initial... OK

管理テーブルができたら、管理ユーザ作成コマンドを実行する。
初回はコマンドでユーザを作るが、
以降は管理画面から追加できる。

python manage.py createsuperuser
Username (leave blank to use 'nagamitsu'): 
Email address: nagamitsu@foo.bar.com
Password: 
Password (again): 
Superuser created successfully.

ユーザを作成したら、サーバを起動して管理画面を起動してみる。

python manage.py runserver

管理画面のURLはこれだ。
http://127.0.0.1:8000/admin
f:id:nagamitsu1976:20170623144059p:plain
先ほど作ったユーザでログインしてみる。
f:id:nagamitsu1976:20170623144349p:plain
ログイン後、管理メニューのようなものが表示される。

Usersなどをクリックしてみるとわかると思うが、
今のところ、管理ユーザの追加・編集程度しかできないが、
定義を加えることで、
作成したアプリのモデルを管理画面から編集することができるようになる。

といったところで、次回へ続く。