ログイン・ログアウトの実装ができたので,ユーザ一覧ページを作成してみよう.
users/views.py
from 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 %}
ユーザ一覧を表示します.