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. 未検証ユーザのパスワードリセット

カスタムユーザ認証

ユーザ一覧ページ

ログイン・ログアウトの実装ができたので,ユーザ一覧ページを作成してみよう.

users/views.pyfrom urllib.parse import urlparse, urlunparse

from django.conf import settings

# Avoid shadowing the login() and logout() views below.
from django.contrib.auth import REDIRECT_FIELD_NAME, get_user_model
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout as auth_logout
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.decorators import login_required
# from django.contrib.auth.forms import (
#    AuthenticationForm,
#    PasswordChangeForm,
#    PasswordResetForm,
#    SetPasswordForm,
# )
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.http import HttpResponseRedirect, QueryDict
from django.shortcuts import resolve_url
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.utils.http import url_has_allowed_host_and_scheme, urlsafe_base64_decode
from django.utils.translation import gettext_lazy as _
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.debug import sensitive_post_parameters
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView

from django.shortcuts import render
# from django.shortcuts import redirect
# from django.urls import reverse
from .forms import AuthenticationForm

UserModel = get_user_model()

# Create your views here.

@login_required(login_url='/users/login/')
def users_index(request):
    # if not request.user.is_authenticated:
    #     return redirect('%s?next=%s' % (reverse('users:login'), request.path))
    context = {}
    context['usermodels'] = UserModel.objects.all()
    return render(request, 'users/index.html', context)

index.html を編集します.

users\templates\users\index.html{% extends "base.html" %}

{% block title %}
ユーザ一覧
{% endblock %}

{% block content %}
<h1 class="my-5">ユーザ一覧</h1>
{% for usermodel in usermodels %}
    <div class="card mb-3">
        <div class="card-body">
            <h5 class="card-title">
                {{ usermodel.username }}
            </h5>
            <p class="card-text">
                email: {{ usermodel.email }}<br>
                Full Name: {{ usermodel.first_name }} {{ usermodel.last_name }}
            </p>
        </div>
    </div>
{% endfor %}
{% endblock content %}

ユーザ一覧を表示します.

django2022-00317

目次に戻る