5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Django]未ログインユーザーをログインページにリダイレクトさせる方法

Last updated at Posted at 2020-11-14

##環境
Python(3.6.2)
Django(2.1.7)

##実装方法
まず下記のモジュールを各アプリのview.pyにインポートします

view.py
from django.contrib.auth.mixins import LoginRequiredMixin

ログインが必須となるページの表示に関わるクラスに下記のように記載します。

view.py
class MypageView(LoginRequiredMixin, generic.ListView):
  template_name = 'index.html'

LoginRequiredMixinは必ず__第一引数に指定__してください。そうしないとログインページにリダイレクトされません。

settings.pyにログインしていない場合のリダイレクト先(ログインページ)を指定します。

settings.py
LOGIN_URL = '/accounts/login/'

これで完了

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?