[PYTHON] Create a Django login screen

Last time, I felt like I couldn't register a new Staff because I didn't get the user information, so I will create a login screen.

By logging in to the staff, we will create it in the staff app. Make sure to load the login template into urls.py.

python:staff.urls.py


from django.urls import path, include
from .views import StaffCreate, loginfunc

urlpatterns = [
    path('login/', loginfunc, name='login'),
    path('create/', StaffCreate.as_view(), name = "staffcreate"),
]

Added loginfunc. Next, create a login funk in Views.

python:staff.views.py


def loginfunc(request):
    if request.method == 'POST':
        user_name = request.POST['username']
        user_password = request.POST['password']
        user = authenticate(request, username=user_name, password=user_password)
        if user is not None:
                login(request, user)
                return redirect('staff:staffcreate')
        else:
                return render(request, 'staff/login.html', {'error':'Wrong employee name or password'})
    return render(request, 'staff/login.html')

Is this all right? From the html login screen, enter the entered username and password in the variable user_name user_password and check if there is the same user. If it's not that there are no users (it's hard to understand (laughs)), log in and call the page named staffcreate with the staff app ursl. Should go like that

If the user is not found, return to the login screen, but then pass the message with an error.

templates/staff/login.html


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

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

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

{% block content %}

<form class="form-signin" method="POST" action="">{% csrf_token %}
    <img class="mb-4" src="/docs/4.5/assets/brand/bootstrap-solid.svg" alt="" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal">Login page</h1>
    {{ error }}
    <label for="text" class="sr-only">Employee ID</label>
    <input type="text" name="username" placeholder="username" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" name="password" placeholder="Password" required>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Login</button>
    <p class="mt-5 mb-3 text-muted">&copy;GK</p>
</form>
{% endblock content %}

I just made some modifications from the bootstrap template. Add settings to settings.py so that you can call in static files

config/settings.py


STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

Create a static folder in the same hierarchy as templates In order to organize, create a staff folder, create loginstyle.css in it, and write css here. This is also a copy and paste from the bootstrap template, so it is still impossible to write from scratch by myself

static/staff/loginstylecss


html,
body {
  height: 100%;
}

body {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: #f5f5f5;
}

.form-signin {
  width: 100%;
  max-width: 330px;
  padding: 15px;
  margin: auto;
}
.form-signin .checkbox {
  font-weight: 400;
}
.form-signin .form-control {
  position: relative;
  box-sizing: border-box;
  height: auto;
  padding: 10px;
  font-size: 16px;
}
.form-signin .form-control:focus {
  z-index: 2;
}
.form-signin input[type="email"] {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

I wonder if this can be done. Save and start the server.

image.png

It was no good staaff / login.html ??? What is this

Many a ... Fixed views

python:staff/login.views.py


def loginfunc(request):
    if request.method == 'POST':
        user_name = request.POST['username']
        user_password = request.POST['password']
        user = authenticate(request, username=user_name, password=user_password)
        if user is not None:
                login(request, user)
                return redirect('staff:staffcreate')
        else:
                return render(request, 'staff/login.html', {'error':'Wrong employee name or password'})
    return render(request, 'staff/login.html')

I will try again with this

What the hell! !! !! !!

image.png

The display is strange ... Isn't it safer not to touch it from bootstarp?

2 hours to make various corrections and cite what I created in the past ... After all, I still don't know ... It looks like the CSS file isn't being called in

image.png

The current state ...

python:config.settings.py


STATIC_URL = '/staic/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

In addition, urls

python:config.urls.py


from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('staff/',include('staff.urls') ),
    path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

I changed it to, created a static folder in the same hierarchy as templates, and created style.css directly under it.

static/style.css


html,
body {
  height: 100%;
}

body {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: #f5f5f5;
}

.form-signin {
  width: 100%;
  max-width: 330px;
  padding: 15px;
  margin: auto;
}
.form-signin .checkbox {
  font-weight: 400;
}
.form-signin .form-control {
  position: relative;
  box-sizing: border-box;
  height: auto;
  padding: 10px;
  font-size: 16px;
}
.form-signin .form-control:focus {
  z-index: 2;
}
.form-signin input[type="email"] {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

Since this is not read, the width does not change at all ...

Let's give up a little and start doing the create function

Recommended Posts

Create a Django login screen
Create a Django schedule
Creating a login screen with Django allauth
A story about implementing a login screen with django
Create a homepage with django
Steps to create a Django project
Create a file uploader with Django
Create a LINE Bot in Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
Create a model for your Django schedule
Build a TOP screen for Django users
Create an update screen with Django Updateview
Redo everything for the Django login screen
Create a dashboard for Network devices with Django!
Create a one-file hello world application with django
How to create a Rest Api in Django
Until you create a new app in Django
Creating a Home screen
Create a Python module
Django2 screen addition flow
Create a Bootable LV
Create a Python environment
Start a Django project
Create Django Todo list
Create a slack bot
A memo to create a virtual environment (venv) before Django
Create a Todo app with Django REST Framework + Angular
I tried to create a table only with Django
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
Display a screen that requires login to digital signage
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Transit to the update screen with the Django a tag
To myself as a Django beginner (1) --Create a project app--
To myself as a Django beginner (4) --Create a memo app--
Create a Todo app with Django ⑤ Create a task editing function
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Create a Wox plugin (Python)
Create a local pypi repository
Create a shogi game record management app using Django 4 ~ Create View ~
Create a function in Python
Create a dictionary in Python
[Python] Create a file & folder path specification screen with tkinter
Create an API with Django
Django beginners create simple apps 3
Django beginners create simple apps 1
Create a (simple) REST server
Create ToDo List [Python Django]
[Django] Make a pull-down menu
[Django] Create a model suitable for phone numbers / zip codes
Create a python numpy array
Create a dummy data file
Django beginners create simple apps 2
Create your own Django middleware
Create a heatmap with pyqtgraph
Create a web API that can deliver images with Django
Create a Todo app with Django ① Build an environment with Docker
Create a classroom on Jupyterhub
Create a simple textlint server