Create a user registration app (register).
python manage.py startapp register
To register an app, write the app class in /crud/
/crud/config/setting.py
INSTALLED_APPS = [
'register.apps.RegisterConfig',
***
]
※reference
/crud/register/apps.py
from django.apps import AppConfig
class RegisterConfig(AppConfig):
name = 'register'
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})
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 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>
python manage.py runserver
Access "http://127.0.0.1:8000/signup/" and confirm that the following screen is displayed.
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'
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>
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})
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
Click the "Submit" button to register the user. After completing the user registration, confirm that the screen will change to the HOME screen.
Check the newly registered user. Access "http://127.0.0.1:8000/admin/auth/user/" and the following screen will be displayed. confirm.
That's all for this time. Thank you very much.