[PYTHON] Redo everything for the Django login screen

I was researching various things from the function, but I made it by imitating it because it became difficult just to create a login screen. However, I recreated the project from scratch (laughs)

image.png

The procedure was created in almost the same way from the reference source! Great article

This kind of article is helpful because Django still has little Japanese information. Then, I think it's like Ruby ... I just chose python because I didn't know the future

Reference source https://intellectual-curiosity.tokyo/2018/11/13/django%E3%81%AE%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E5%87%A6%E7%90%86%E3%82%92%E5%AE%9F%E8%A3%85%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%E2%91%A0/

The site here was the best!

I created a login screen by following this procedure.

image.png

When you log in

image.png

When you log out, you will be returned to the login screen.

If you are not logged in, you will be forced to return to the login screen.

I'll write the code

python:accounts.urls.py


from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
from django.urls import path, include

app_name = 'accounts'

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('home/', views.home, name="home"),
]

python:accounts.views.py


from django.shortcuts import render
from django.contrib.auth.decorators import login_required

@login_required
def home(request):
    return render(request, 'accounts/home.html')

python:config.setting.py


LOGIN_REDIRECT_URL = '/accounts/home'
LOGOUT_REDIRECT_URL = '/accounts/home'

add to

templates/accounts/base.html


<!doctype html>
<html lang="ja">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">

    {% block customcss %}
    {% endblock customcss %}
    
    <title>related family</title>
  </head>
  <body>
  
    {% block header %}
    {% endblock header %}

    {% block content %}
    {% endblock content %}

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
  </body>
</html>

HTML for login

templates/accounts/login.html


{% extends 'accounts/base.html' %}
{% load static %}


{% block customcss %}
    <link rel='stylesheet' type='text/css' href="{% static 'css/accounts/style.css' %}">
{% endblock customcss %}


{% block header %}
{% endblock header %}

{% block content %}
  <body>
    
    <form class="form-login" action='' method='POST'>
    <h1>Login screen</h1>
    {% csrf_token %}

    {{ form.as_p }}

    <button>Login</button>
    <p class="mt-5 mb-3 text-muted">&copy;GK</p>
    </form>
  </body>
</html>
{% endblock content %}

templates/accounts/home.html


{% extends 'accounts/base.html' %}
{% load static %}

{% block customcss %}
    <link rel='stylesheet' type='text/css' href="{% static 'css/style.css' %}">
{% endblock customcss %}


{% block header %}
<title>Home Screen</title>
{% endblock header %}

{% block content %}
<body>
  {% if user.is_authenticated %}
  Hi {{ user.username }}!
  <p><a href="{% url 'accounts:logout' %}">logout</a></p>
  {% else %}
  <p>You are not logged in</p>
  <a href="{% url 'accounts:login' %}">login</a>
{% endif %}
  </body>
{% endblock content %}

The login screen is now implemented. From now on, I will investigate how to link information to user and staff information that I originally wanted to do.

Recommended Posts

Redo everything for the Django login screen
Create a Django login screen
Build a TOP screen for Django users
Creating a login screen with Django allauth
Recreate the django staff registration screen with class
Django 1.9 for internationalization
[Django] Redo migrate
A story about implementing a login screen with django
DJango Memo: From the beginning (Error screen settings)
Learning notes for the migrations feature in the Django framework (2)
Learning notes for the migrations feature in the Django framework (3)
Learning notes for the migrations feature in the Django framework (1)
Transit to the update screen with the Django a tag
[Django] How to redirect unlogged-in users to the login page
Django2 screen addition flow
[Django] Rename the project
Summary of stumbling blocks in Django for the first time
Django Make choices only for the facility you belong to
DJango Memo: From the beginning (more edits to the management screen)
[Python] Create a screen for HTTP status code 403/404/500 with Django