Python Django 入門トップページ


カスタムユーザ認証

  1. プロジェクトの概要
  2. プロジェクトの作成と初期設定
  3. Users アプリケーションの作成と有効化
  4. 認証にカスタムユーザモデルを利用する
  5. モデルの作成
  6. マイグレーション
  7. ユーザの登録
  8. 管理ユーザの登録
  9. 管理サイトの作成
  10. Comments アプリケーションの作成
  11. ページ雛形の作成
  12. ログイン・ログアウトの実装
  13. Navbar の設置
  14. Comments アプリケーションのユーザ認証
  15. ユーザ一覧ページ
  16. ユーザ詳細情報の表示
  17. ユーザ情報の更新
  18. パスワードの変更
  19. Gmail 2段階認証の設定とアプリパスワードの取得
  20. メールの設定と送信
  21. パスワードのリセット
  22. ユーザ登録機能の実装
  23. ユーザ登録時に氏名も登録
  24. ユーザ登録時にメールアドレスも登録
  25. ユーザ登録してもログインできないように
  26. ユーザ登録後にメールを送信
  27. メール検証によるアカウントの有効化
  28. トークン有効期限の変更
  29. ログアウト後に top へリダイレクト
  30. 検証メールの再送信
  31. 未検証ユーザのログインエラーメッセージ
  32. メールに有効期限を表示
  33. フラッシュメッセージの変更
  34. 未検証ユーザのパスワードリセット

カスタムユーザ認証

プロジェクトの作成と初期設定

プロジェクトの作成

まず,Anaconda prompt を起動し django_comment_auth という名称のプロジェクトを作成します.

(py39) C:\Users\lecture>cd Documents ⏎

(py39) C:\Users\lecture\Documents>cd django ⏎

(py39) C:\Users\lecture\Documents\django>django-admin startproject custom_auth_project ⏎

(py39) C:\Users\lecture\Documents\django>cd custom_auth_project ⏎

(py39) C:\Users\lecture\Documents\django\custom_auth_project>dir ⏎
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は E033-4666 です

 C:\Users\lecture\Documents\django\custom_auth_project のディレクトリ

2022/08/08  11:56    <DIR>          .
2022/08/08  11:56    <DIR>          ..
2022/08/08  11:56    <DIR>          custom_auth_project
2022/08/08  11:56               697 manage.py
               1 個のファイル                 697 バイト
               3 個のディレクトリ  13,731,504,128 バイトの空き領域

(py39) C:\Users\lecture\Documents\django\custom_auth_project>

Git を初期化して最初のコミットを行います.

(py39) C:\Users\lecture\Documents\django\custom_auth_project>git init ⏎
Initialized empty Git repository in C:/Users/lecture/Documents/django/custom_auth_project/.git/

(py39) C:\Users\lecture\Documents\django\custom_auth_project>git add . ⏎

(py39) C:\Users\lecture\Documents\django\custom_auth_project>git commit -m"initial commit" ⏎
[master (root-commit) 978ee3d] initial commit
 6 files changed, 198 insertions(+)
 create mode 100644 custom_auth_project/__init__.py
 create mode 100644 custom_auth_project/asgi.py
 create mode 100644 custom_auth_project/settings.py
 create mode 100644 custom_auth_project/urls.py
 create mode 100644 custom_auth_project/wsgi.py
 create mode 100644 manage.py

(py39) C:\Users\lecture\Documents\django\custom_auth_project>

Visual Studio Code を起動して,プロジェクトのフォルダを開きます.Anaconda prompt (またはコマンドプロンプト)でプロジェクトのディレクトリから code . コマンドを使うと簡単に起動することができるはずです.Mac で Homebrew を使って Visual Studio Code をインストールした場合も同じように code . コマンドが使えます.インストーラを使ってインストールした場合は,PATH 内に 'code' コマンドをインストールしておくことでやはり code . コマンドが使えるようになります.

(py39) C:\Users\lecture\Documents\django\custom_auth_project>code . ⏎

(py39) C:\Users\lecture\Documents\django\custom_auth_project>

Visual Studio Code を使って,.gitignore ファイルを作成します.このファイル内に記載されたファイルは git の管理下には置かれないようになります.例えば,データベースのファイル db.sqlite3 は git で管理しないことを意味します.なお,Windows 環境では .DS_Store は不要です.

.gitignoredb.sqlite3
*/__pycache__/
*/*/__pycache__/
.coverage
htmlcov
.DS_Store

.gitignore ファイルを作成したら,git でコミットしておきます.

(py39) C:\Users\lecture\Documents\django\custom_auth_project>git status ⏎
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore

nothing added to commit but untracked files present (use "git add" to track)

(py39) C:\Users\lecture\Documents\django\custom_auth_project>git add . ⏎

(py39) C:\Users\lecture\Documents\django\custom_auth_project>git commit -m"gitignore" ⏎
[master 1ea201c] gitignore
 1 file changed, 6 insertions(+)
 create mode 100644 .gitignore

(py39) C:\Users\lecture\Documents\django\custom_auth_project>

プロジェクトの初期設定

次にプロジェクトの初期設定を行います.

custom_auth_project/settings.py (抜粋)# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'

USE_I18N = True

USE_TZ = False

Web サーバの起動

Web サーバを起動して,トップページにアクセスします.ロケット打ち上げのページが日本語で表示されたら成功です.

(py39) C:\Users\lecture\Documents\django\custom_auth_project>python manage.py runserver ⏎
Watching for file changes with StatReloader
Performing system checks...

django2022-00301

以降のステップでは明記しませんが,途中の段階のきりの良いところでは git でコミットしておくことをおすすめします.

目次に戻る