A super introduction to Django by Python beginners! Part 6 I tried to implement the login function

About this article

This is the output page of the result of learning about Django on Udemy. The previous article is here .

This time, we will implement the login function in the diary application created last time.

Deliverables

TOP page before login (List and login are displayed in the navigation bar.)

TOP page after login (List, add and logout are displayed in the navigation bar.)

Login screen

Logout screen

settings.py Add LOGIN_URL to settings.py. This time, the login screen of the admin site will be used, so let's use admin: login. Add the following code to settings.py.

settings.py


LOGIN_URL = 'admin:login'

views.py This time, I would like to control the add / update / delete page so that login is required.

First edit views.py. Import LoginRequiredMixin. Then, inherit LoginRequiredMixin to the class you want to log in to. This is all you need to do to implement the login function. The rest is HTML editing.

views.py


from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render, redirect, get_object_or_404
from .forms import DayCreateForm
from .models import Day
from django.views import generic
from django.urls import reverse_lazy

class IndexView(generic.ListView):
    model = Day
    paginate_by = 3

class DayCreateView(LoginRequiredMixin, generic.CreateView):
    model = Day
    form_class = DayCreateForm
    success_url = reverse_lazy('diary:index')

class DayUpdateView(LoginRequiredMixin, generic.UpdateView):
    #It has almost the same contents as CreateView, but passes not only form but also Day object.
    model = Day
    form_class = DayCreateForm
    success_url = reverse_lazy('diary:index')

class DayDeleteView(LoginRequiredMixin, generic.DeleteView):
    model = Day
    success_url = reverse_lazy('diary:index')

class DayDetailView(generic.DetailView):
    model = Day

Editing base.html

Next is HTML editing.

If you want to show something only after logging in, you can do it by enclosing it in the following if statement. {% if user.is_superuser%} What you want to see after logging in {% endif%}

If you write as below, add and logout will be displayed after login, The login is displayed before login.

If you set target = "_ blank", login / logout will open in a separate tab.

base.html


    <div class="container">
      <nav class="nav">
        <a class="nav-link active" href="{% url 'diary:index' %}">List</a>
        {% if user.is_superuser %}
        <a class="nav-link" href="{% url 'diary:add' %}">add to</a>
        <a class="nav-link" href="{% url 'admin:logout' %}" target="_blank">Log out</a>        
        {% else %}
        <a class="nav-link" href="{% url 'admin:login' %}" target="_blank">Login</a>        
        {% endif %}
      </nav>
      {% block content %}
      {% endblock %}
    </div>

Operation check

Check the operation with py manage.py runserver. If you click the update / delete button, you will be taken to the login screen.

Recommended Posts

A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
A super introduction to Django by Python beginners! Part 1 I tried to display an HTML page that only says "Hello World"
A super introduction to Django by Python beginners! Part 5 I made a super simple diary application with a class-based general-purpose view
I tried to implement the mail sending function in Python
A super introduction to Django by Python beginners! Part 4 I made a super-simple diary application (created only with functions without using a class-based general-purpose view)
I tried to implement a pseudo pachislot in Python
[Python] I tried to get the type name as a string from the type function
Introduction to AI creation with Python! Part 2 I tried to predict the house price in Boston with a neural network
[Introduction] I tried to implement it by myself while explaining the binary search tree.
[Introduction] I tried to implement it by myself while explaining to understand the binary tree
I tried to implement a one-dimensional cellular automaton in Python
[Django] I tried to implement access control by class inheritance.
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
I also tried to imitate the function monad and State monad with a generator in Python
I tried to solve the ant book beginner's edition with python
[Introduction to Python] How to split a character string with the split function
[Python] I tried to implement stable sorting, so make a note
[Introduction to simulation] I tried playing by simulating corona infection ♬ Part 2
I tried to implement a misunderstood prisoner's dilemma game in Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to implement PLSA in Python
I tried to implement permutation in Python
A super introduction to Python bit operations
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried to communicate with a remote server by Socket communication with Python.
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to verify and analyze the acceleration of Python by Cython
[Introduction to Python] How to write a character string with the format function
I tried to verify the result of A / B test by chi-square test
Python: I want to measure the processing time of a function neatly
I tried to analyze the New Year's card by myself using python
I tried to implement a card game of playing cards in Python
I made a function to see the movement of a two-dimensional array (Python)
[Python] I tried substituting the function name for the function name
I tried to implement TOPIC MODEL in Python
I tried using the Datetime module by Python
I tried to implement selection sort in python
I tried to implement the traveling salesman problem
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 5/22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part7 / 22]
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 4/22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part3 / 22]
[Python] Smasher tried to make the video loading process a function using a generator
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 1/22]
I tried to implement what seems to be a Windows snipping tool in Python
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 6/22]
[Introduction to Docker] I tried to summarize various Docker knowledge obtained by studying (Windows / Python)
Introduction to AI creation with Python! Part 3 I tried to classify and predict images with a convolutional neural network (CNN)
python beginners tried to predict the number of criminals
I tried to graph the packages installed in Python
I want to easily implement a timeout in python
I tried to create a table only with Django
[Introduction to Python] How to iterate with the range function?
I tried to implement Minesweeper on terminal with python
I tried to touch the CSV file with Python