Help us understand the problem. What is going on with this article?

Django でユーザー認証

More than 1 year has passed since last update.

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 staticfiles %}
<!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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした