LoginSignup
5
3

More than 1 year has passed since last update.

Django でユーザー認証

Last updated at Posted at 2018-12-10

Django でユーザー認証を行う方法です。

元のプロジェクトは、
Django で テンプレートを使う
に 次のようにして MariaDB を使えるようにしたものです。
Django で MariaDB を使う

次のようにユーザーが出来ているものとします。
user.png

まず、テンプレートに追加です。
次の3つのファイルを作成します。

templates/registration/base.html
templates/registration/login.html
templates/registration/logged_out.html

フォルダーの作成

mkdir templates/registration
templates/registration/base.html
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Django System</title>
</head>
<body>
<div class="container">
    <div class="content">
        {% block content %}{% endblock %}
    </div>
</div>
</body>
</html>
templates/registration/login.html
{% extends 'registration/base.html' %}

{% block title %}Login{% endblock %}

{% block content %}
<h1>Login</h1>
<section class="common-form">
    {% if form.errors %}
    <p class="error-msg">Your username and password didn't match. Please try again.</p>
    {% endif %}

    {% if next %}
    {% if user.is_authenticated %}
    <p class="error-msg">Your account doesn't have access to this page. To proceed,
        please login with an account that has access.</p>
    {% else %}
    <p class="error-msg">Please login to see this page.</p>
    {% endif %}
    {% endif %}

    <form method="post" action="{% url 'login' %}">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="submit">Login</button>
        <input type="hidden" name="next" value="{{ next }}"/>
    </form>
</section>
{% endblock %}
templates/registration/logged_out.html
{% extends 'registration/base.html' %}

{% block title %}Logout{% endblock %}

{% block content %}
<h1>Logged Out</h1>
<p>Thanks for spending some quality time with the Web site today.</p>
<p><a href="{% url 'login' %}">Log in again</a></p>
{% endblock %}

views.py を修正します。

次を追加します。
from django.contrib.auth.decorators import login_required
@login_required

home/views.py
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required

@login_required
def index(request):
    str_out = ""
    str_out += "*** home *** start ***<p />"
    str_out += "ホームです。<p />"
    str_out += "<a href='app01/'>app01</a><p />"
    str_out += "<a href='app02/'>app02</a><p />"
    str_out += "*** Dec/10/2018 PM 16:58 ***<p />"
    str_out += "*** home *** end ***<p />"
    return HttpResponse(str_out)

次も同様に修正します。

app01/views.py
app02/views.py

accounts というアプリを作成

python manage.py startapp accounts

proj01/urls.py を修正

proj01/urls.py
from django.contrib import admin
from django.urls import path
from django.urls import include

urlpatterns = [
    path('accounts/', include('django.contrib.auth.urls')),
    path('', include('home.urls')),
    path('app01/', include('app01.urls')),
    path('app02/', include('app02.urls')),
    path('admin/', admin.site.urls),
]

開発サーバーを起動してテスト

python manage.py runserver

http://127.0.0.1:8000/ にアクセス
login.png

Logout を実装するには、

proc01/settings.py
省略
LOGIN_REDIRECT_URL = '/'

ページに Logout のリンクを追加します。

home/views.py
省略
    str_out += '<a href="accounts/logout/">Logout</a><p />'
省略
templates/app01.html
(省略)
<a href="../accounts/logout/">Logout</a><p />
(省略)
templates/app02.html
(省略)
<a href="../accounts/logout/">Logout</a><p />
(省略)

開発サーバーを起動してテスト

python manage.py runserver

logout_01.png

logout_02.png

5
3
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
5
3