[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~

Series list (will be updated when all articles are completed)

Add user registration app

Create an app

Create a user registration app (register).

python manage.py startapp register

Register your app with Django

To register an app, write the app class in /crud//apps.py in INSTALLED_APPS in /crdu/config/setings.py.

/crud/config/setting.py


INSTALLED_APPS = [
    'register.apps.RegisterConfig',
    ***
]

※reference

/crud/register/apps.py


from django.apps import AppConfig


class RegisterConfig(AppConfig):
    name = 'register'

Create Views

It means to execute the signup function with request as an argument and return /crud/register/templates/register/signup.html which rendered the form.

/crud/register/views.py


from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm

# Create your views here.


def signup(request):
    form = UserCreationForm()
    return render(request, 'register/signup.html', {'form': form})

Set URLConf

URLConf refers to Django's URL management feature, which is managed in the urls.py file. URLConf can be centrally managed by PJ's urls.py, but this time I would like to manage the screen transition in the application with the application's URLConf and place the PJ's URLConf in front of it.

That is, include the app's URLConf within the PJ's URLConf and implement the url and view mapping in the app's URLConf.

** PJ URLConf ** This means that you should include the register app urls.py so that you can return access to "http: // *** /".

/crud/config/urls.py


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

urlpatterns = [
    ***
    path('', include('register.urls')),
    ***
]

** App URL Conf ** Accessing "http: // *** / signup /" means that you should return the signup function in views.py.

/crud/register/urls.py


from django.urls import path
from . import views

urlpatterns = [
    path('signup/', views.signup, name='signup'),
]

Create test HTML

Create the associated HTML in views.py. First, write HTML that does not use arguments (does not render) as test HTML.

/crud/register/templates/register/signup.html


<h1>signup</h1>

Operation check (test HTML)

python manage.py runserver

Access "http://127.0.0.1:8000/signup/" and confirm that the following screen is displayed. image.png

Create a form

Create a user registration form. You can use HTML input tags, but Django has a handy django-crispy-forms.

Install django-crispy-forms

pipenv install django-crispy-forms

Edit setting.py to apply django-crispy-forms to Django.

/crud/config/setting.py


INSTALLED_APPS = [
    ***
    'crispy_forms',
]

CRISPY_TEMPLATE_PACK = 'bootstrap4'

Modify HTML

Apply django-crispy-forms to your HTML. Write {{form | crispy}}. Don't forget to load {% load crispy_forms_tags%}.

/crud/register/templates/register/signup.html


{% extends 'register/base.html' %}

{% load static %}
{% load crispy_forms_tags %}

<body class="main-layout">
    {% block content %}
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-5">
                <form method="POST">
                    {% csrf_token %}
                    {{ form|crispy }}
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
    </div>
    {% endblock content %}
</body>

Fix URLConf

For the POST method, redirect to home. If they are different, the input data will be inherited and the user registration page will be displayed again.

/crud/register/views.py


from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm

# Create your views here.


def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('blog-home')
    else:
        form = UserCreationForm()
    return render(request, 'register/signup.html', {'form': form})

Operation check

python manage.py runserver

Access "http://127.0.0.1:8000/signup/" and confirm that the following screen is displayed.

Enter the following: Username: testuser1 Password: testpass Password (for confirmation): testpass

image.png

Click the "Submit" button to register the user. After completing the user registration, confirm that the screen will change to the HOME screen. image.png

Check the newly registered user. Access "http://127.0.0.1:8000/admin/auth/user/" and the following screen will be displayed. confirm. image.png

That's all for this time. Thank you very much.

Recommended Posts

[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[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 Todo app with the Django REST framework
Create a python GUI using tkinter
Create a Python general-purpose decorator framework
Install Python framework django using pip
Create a CRUD API using FastAPI
Try using the Python web framework Django (2) --Look at setting.py
Implementation of CRUD using REST API with Python + Django Rest framework + igGrid
Create a graph using the Sympy module
[Python] Create a Batch environment using AWS-CDK
[Ev3dev] Create a program that captures the LCD (screen) using python
Create a REST API to operate dynamodb with the Django REST Framework
Create a shogi game record management app using Django 3 ~ Django default management site settings ~
Try using the Python web framework Django (1)-From installation to server startup
Create a record with attachments in KINTONE using the Python requests module
Create a GIF file using Pillow in Python
Run a Python file from html using Django
Create a Mac app using py2app and Python3! !!
Create a MIDI file in Python using pretty_midi
Create a GUI on the terminal using curses
Create a Django schedule
Django python web framework
Create a Python module
Create a Python environment
Create a Python image in Django without a dummy image file and test the image upload
Create a Django project and application in a Python virtual environment and start the server
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
Create a data collection bot in Python using Selenium
Create a Todo app with Django REST Framework + Angular
Cut a part of the string using a Python slice
(Python) Try to develop a web application using Django
[Python] Create a ValueObject with a complete constructor using dataclasses
DJango Note: From the beginning (using a generic view)
Build a Python virtual environment using venv (Django + MySQL ①)
[Django Rest Framework] Customize the filter function using Django-Filter
Create a company name extractor with python using JCLdic
A little bit from Python using the Jenkins API
Create a dictionary by searching the table using sqlalchemy
[Python] Let's change the URL of the Django administrator site
Create a dictionary in Python
[S3] CRUD with S3 using Python [Python]
Create ToDo List [Python Django]
Create a homepage with django
Create JIRA tickets using Python
Create a python numpy array
Create a Django login screen
Create a directory with python
Create a shogi game record management app using Django 4 ~ Create View ~
Create a local scope in Python without polluting the namespace
Probably the easiest way to create a pdf with Python3
Try a similar search for Image Search using the Python SDK [Search]
Create a real-time auto-reply bot using the Twitter Streaming API
How to generate a query using the IN operator in Django
Create a Twitter BOT with the GoogleAppEngine SDK for Python
[Python] Create a screen for HTTP status code 403/404/500 with Django
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
Control the motor with a motor driver using python on Raspberry Pi 3!