I honestly don't know form.py at all and use it for the first time. I read multiple articles on the net, but I couldn't think of it at all ... (laughs)
However, I will write it for the time being.
form.py
from django import forms
from .models import Staff
class CreateUserForm(forms.ModelForm):
#Inherit modelform
class meta():
model = Staff
fields = ("name ", "password", "roll", "nyushabi", "taishabi", "hyoujijyun", "jikyu", "delete ")
With this, it seems that it will be linked to html.
Next, create Views.
views.py
from django.shortcuts import render
from . import CreateUserForm
def form_name_view(request):
form = forms.UserForm()
if request.method == 'POST':
form = forms.CreateUserForm(request.POST)
if form.is_valid():
form.save()
return render(request, 'templates/staff.create.html', {'CreateUserForm': form})
if request.method=="POST" Is a command that operates when the registration button is pressed after inputting something because it is a request that is not the first display.
If it is not Post, it is an image that calls the template and calls CreateUserForm from Foms. I don't know if this is really okay (laughs)
Next, create a template. First, copy base.html from bootstarp and modify a part.
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.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<title>
{% block title %}
{% endblock title %}
</title>
</head>
<body>
{% blcok 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-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
This completes the creation of the base part.
HTML for registration is
python:staff.create.py
{% extends 'staff/base.html' %}
{% block content %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Submit">
{% block content %}
This should load it from the base, plug it into the content, and display it.
Finally, fix urls.py
python:staff.url.py
from django.urls import path, include
from .views import createstaff
urlpatterns = [
path('create/', createstaff, name = "createstaff"),
]
Now, let's start the server and try to see if it can be displayed once.
It's an error that forms cannot be found on the 5th line of views.
staff.views
from django.shortcuts import render
from .form import CreateUserForm
def createstaff(request):
form = CreateUserForm.UserForm()
if request.method == 'POST':
form = forms.CreateUserForm(request.POST)
if form.is_valid():
form.save()
return render(request, 'templates/staff.create.html', {'CreateUserForm': form})
I will try again with this.
It's still useless.
After this, I tried playing with it a little, but I could not solve the error, so I will stop using form and switch to creating with class.
Recommended Posts