[Django] How to redirect unlogged-in users to the login page

environment

Python(3.6.2) Django(2.1.7)

Implementation method

First, import the following modules into view.py of each app

view.py


from django.contrib.auth.mixins import LoginRequiredMixin

Describe as follows in the class related to the display of pages that require login.

view.py


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

Be sure to specify LoginRequiredMixin in the __first argument __. Otherwise you will not be redirected to the login page.

Specify the redirect destination (login page) when you are not logged in to settings.py.

settings.py


LOGIN_URL = '/accounts/login/'

This completes

Recommended Posts

[Django] How to redirect unlogged-in users to the login page
How to check the version of Django
How to print debug messages to the Django console
[Wagtail] Add a login page to the Wagtail project
How to use the generator
How to use the decorator
How to increase the axis
How to start the program
How to write custom validations in the Django REST Framework
How to generate a query using the IN operator in Django
How to do the initial setup from Django project creation
I can't log in to the admin page with Django3
In Django, how to abbreviate the long displayed string as ....
[Django] Redirect to previous page after submitting with POST form
How to calculate the autocorrelation coefficient
How to use the zip function
How to use the optparse module
django + nginx How to make images viewed only by logged-in users
How to read the SNLI dataset
Try Ajax on the Django page
[Python] How to import the library
How to reflect CSS in Django
How to get started with Django
Deploy the Django tutorial to IIS ①
How to overwrite the output to the console
How to write Django1.9 environment-independent wsgi.py
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to build an application from the cloud using the Django web framework
How to pass values to JavaScript variables directly from the [Django] template tag
How to make only one data register on the Django admin screen
How to display the progress bar (tqdm)
How to use the Spark ML pipeline
How to solve the bin packing problem
How to set the server time to Japanese time
How to delete expired sessions in Django
[Django] Want to customize your admin page?
[Linux] How to use the echo command
How to use the Linux grep command
How to get colored output to the console
How to operate Linux from the console
Django non-logged-in users want to transition to login.html
How to access the Datastore from the outside
How to use the IPython debugger (ipdb)
How to test on a Django-authenticated page
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
Redo everything for the Django login screen
How to uniquely identify the source of access in the Django Generic Class View
[Django memo] I want to set the login user information in the form in advance
How to count the number of elements in Django and output to a template
I'm addicted to the difference in how Flask and Django receive JSON data
How to add pre-save processing when adding objects on the Django admin site
How to assign multiple values to the Matplotlib colorbar
How to calculate the volatility of a brand
How to use the C library in Python
How to specify the launch browser for JupyterLab 3.0.0
How to find the area of the Voronoi diagram
The easiest way to get started with Django
How to specify the NIC to scan with amazon-dash
How to implement Rails helper-like functionality in Django