Recreate the django staff registration screen with class

I tried to create a registration screen with the def function, but it didn't work, so I switched to creating it with class.

There are some parts that you have to create with this, and some parts that you don't, so don't be too particular about screens that you don't use often.

python:staff.urls.py


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

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

Also change the routing of the urls. In the case of a class, as_view () seems to be a rule.

And also changed Views

views.py


from django.shortcuts import render
from .form import CreateUserForm
from .models import Staff
from django.views.generic import CreateView


class StaffCreate(CreateView):
    template_name = "staff/create.html"
    model = Staff
    fields = ("id", "name", "password", "roll", "nyushabi", "taishabi", "hyoujijyun", "jikyu", "delete")
    ##success_url = reverse_lazy("list")

When the registration is successful, I will return to the stafflist screen, but I have not created it yet, so I commented it out.

Finally, modify the html file.

python:staff.create.html


{% extends 'base.html' %}

{% block title %}
{% endblock title %}

{% block header %}
Staff registration
{% endblock header %}

{% block content %}
<from action="" method="POST">
    {{ form.as_p }}
    <input type="submit" value="Registration">
</from>
{% endblock content %}

This should be done ... I always try to fix it with an error from here.

Start the server http://127.0.0.1:8000/staff/create/

Let's open the page with.

image.png

Oh, it was displayed! It's a simple and clunky screen, but it's important that the function works! (I hope I can wear the decorations here in the future, but first I will implement the functions!)

I will try to register.

image.png

Well, the date is also solid, it is difficult to understand how to enter it, and correction may be essential (laugh) You can experience it by typing it yourself. Let's make a loving system

But for the time being, click the registration button

that? There is no reaction I wonder why ...

do not know…

If you recheck models, it is difficult to know the creation date and time, so correct it. Maybe it's because I haven't registered as a user ...

staff.py


from django.db import models

class Staff(models.Model):
    id = models.AutoField(verbose_name='Employee ID',primary_key=True)
    password = models.CharField(verbose_name='password',max_length=20)
    name = models.CharField(verbose_name='Employee name',max_length=20, blank=False, null=True)
    roll = models.CharField(verbose_name='Position',max_length=10, blank=False, null=True)
    nyushabi = models.DateField(verbose_name='Hire date',blank=False, null=True)
    taishabi = models.DateField(verbose_name='Leaving date',blank=True, null=True)
    hyoujijyun = models.IntegerField(verbose_name='Display order',unique=True)
    jikyu = models.IntegerField(verbose_name='Hourly wage',blank=False, null=True)
    delete = models.BooleanField(verbose_name='Delete flag',default=False)
    create_date = models.DateTimeField(verbose_name='Registered Date', auto_now_add=True)
    create_user = models.CharField(verbose_name='Registered user', max_length=50, null=True)
    update_date = models.DateTimeField(verbose_name='Update date and time', auto_now=True)
    update_user = models.CharField(verbose_name='Update user', max_length=50, null=True)

    def __str__(self):
        return self.name

regist ⇒ Changed to create. This is easier to understand

Perform migration

Also, I forgot to add cross-site countermeasures, so I added it

python:staff.create.html


{% extends 'base.html' %}

{% block title %}
{% endblock title %}

{% block header %}
Staff registration
{% endblock header %}

{% block content %}
<from action="" method="POST">{% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Registration">
</from>
{% endblock content %}

Well, I haven't changed anything so it doesn't seem like the registration works. I will try it.

image.png

I will press the registration button

image.png

No reaction (laughs)

Well, I don't know the cause ...

I wondered if I could understand something if I registered on the administrator screen, but when I tried it, the following error

image.png

Registered users and updated users are required ... That means that the login screen is created first, then you log in to get the user and use it when registering. Good to notice: smiley:

I will suspend the creation of the staff creation screen and create the login screen.

Recommended Posts

Recreate the django staff registration screen with class
Transit to the update screen with the Django a tag
[Django] Display registration data associated with users on the registration / edit form (Form)
Creating a login screen with Django allauth
Create an update screen with Django Updateview
Redo everything for the Django login screen
The easiest way to get started with Django
Build the fastest Django development environment with docker-compose
A story about implementing a login screen with django
DJango Memo: From the beginning (Error screen settings)
Internationalization with django
CRUD with Django
Make Fatjar by changing the main class with Gradle
Add the attribute of the object of the class with the for statement
Hit the Twitter API after Oauth authentication with Django
Create a Todo app with the Django REST framework
The story that Japanese output was confused with Django
How to register the same data multiple times with one input on the Django management screen