[PYTHON] Implement the Django user extension and register the attached information

Last time, I was able to implement login and logout with the user that Django has.

I did a lot of research to extend the user information from there, but in the end it often didn't work.

Finally, it's a setting that feels like it's done.

config/settings.py




INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts.apps.AccountsConfig',
]


LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'

STATIC_URL = '/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
LOGIN_REDIRECT_URL = '/accounts/home'
LOGOUT_REDIRECT_URL = '/accounts/home'

#AUTH_USER_MODEL = 'users.CustomUser'

settings/urls.py


from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('accounts.urls')),
]

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"),
]

accounts/views.py


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

# Create your views here.
@login_required 
def home(request):
    return render(request, 'accounts/home.html')

accounts/admin.py


from django.contrib import admin
from .models import Profile

# Register your models here.
admin.site.register(Profile)

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>

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 }}!
  <h2>{{ user.get_full_name }}</h2>
  <ul>
    <li>name: {{ user.username }}</li>
    <li>Position: {{ user.profile.roll }}</li>
    <li>Hire date: {{ user.profile.nyushabi }}</li>
    <li>Leaving date: {{ user.profile.taishabi }}</li>
    <li>Display order: {{ user.profile.hyoujijyun }}</li>
    <li>Hourly wage: {{ user.profile.jikyu }}</li>
    <li>Delete flag: {{ user.profile.delete }}</li>
    <li>Registered Date: {{ user.profile.create_date }}</li>
    <li>Registered user: {{ user.profile.create_user }}</li>
    <li>Update date and time: {{ user.profile.update_date }}</li>
    <li>Update user: {{ user.profile.update_user }}</li>
  </ul>

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

For the first time here, I did mekumigrate.

Try logging in as the admin user

image.png

image.png

Let's add the extended information to the administrator.

And try logging in

image.png

image.png

Oh, it looks like you can get the attached information

Add a user and see if it really works differently.

I'm trying to get another information by adding a president named Hanako ...

image.png

I can go.

What I noticed here ... It may be duplicated because the flag of valid or invalid is in the standard item of user. In the future, it may be deleted because it is unnecessary.

For the time being, I will proceed with this.

Well, I don't know what I was trying to do and got stuck in the login (laughs). I remember, I started thinking about creating a new staff registration screen, If I can register and edit on the administrator screen, I think that it will not be operational for a while. I would like to implement a different function.

Recommended Posts

Implement the Django user extension and register the attached information
Judge the extension and download the image
Output user information etc. to Django log
Implement a Custom User Model in Django
Specify the Django http status code and return.
Pass login user information to view in Django
[Keras] Implement noisy student and check the effect
Register words in the GiNZA (SudachiPy) user dictionary