2
0

More than 3 years have passed since last update.

【Python】Djangoで遊ぶ(管理画面)

Posted at

この記事について

前回の記事でDjangoのセットアップをしてました。
【Python】Djangoで遊ぶ(セットアップ・HTMLファイル表示)

今回はデフォルトでセットアップされる管理サイト(adminサイト)を少し触れてみます。

環境

OS:Windows10 Home
Python:3.9.2
Django:3.1.7

管理サイト(adminサイト)とは

今回はローカル環境http://127.0.0.1で構築したのですが、何も触ってなくてもhttp://127.0.0.1/adminにアクセスすると以下のページが表示されます。
image.png

ただ、ユーザー名・パスワードは分かりません。。
作成してログインしてみましょう。

アカウント作成

以下のコマンドでアカウント作成ができます。

python manage.py createsuperuser

すると何かエラーを吐きました・・・・

$ python manage.py createsuperuser

You have 18 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.
Traceback (most recent call last):
  File "C:\python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: auth_user

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Works\Dev\Django\demosite\manage.py", line 22, in <module>
    main()
  File "C:\Works\Dev\Django\demosite\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\python39\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\python39\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\python39\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "C:\python39\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 100, in handle
    default_username = get_default_username()
  File "C:\python39\lib\site-packages\django\contrib\auth\management\__init__.py", line 140, in get_default_username
    auth_app.User._default_manager.get(username=default_username)
  File "C:\python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\python39\lib\site-packages\django\db\models\query.py", line 425, in get
    num = len(clone)
  File "C:\python39\lib\site-packages\django\db\models\query.py", line 269, in __len__
    self._fetch_all()
  File "C:\python39\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\python39\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\python39\lib\site-packages\django\db\models\sql\compiler.py", line 1156, in execute_sql
    cursor.execute(sql, params)
  File "C:\python39\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\python39\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\python39\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_user

アカウント作成の前にmigrateをしてくれということで、以下のコマンドでmigrateします。

python manage.py migrate
$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, 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 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 auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

成功しました。

デフォルトではdb.sqlite3にテーブルが作成されます。
この設定はsetting.pyの以下に記載されています。

setting.py
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

ちなみにdb.sqlite3はこのような感じでテーブルが作成されています。
image.png

テーブルが作成されたので、改めてユーザーを作成します。

python manage.py createsuperuser

ユーザー名、メールアドレス、パスワードを入力します。
メールアドレスは未登録でも大丈夫ないようです。
これでアカウントが作成できました。

ログイン

アカウントを作成したのでログインします。
image.png

デフォルトでグループとユーザーの設定を行う機能が使えるみたいです。
グループを設定するときのパーミッションも多くありますね。
image.png

最後に

今回はここまでです。
DBを変更したり、管理する項目を増やしたりするのはまた次の機会にでもします。

2
0
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
2
0