[PYTHON] Try using Django templates.html

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.

image.png

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.

image.png

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

Try using Django templates.html
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using geopandas
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Try using django-import-export to add csv data to django
Try using virtualenv (virtualenvwrapper)
Try Debian + Python 3.4 + django1.7 ...
Try using virtualenv now
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Try using Tweepy [Python2.7]
Try using Pytorch's collate_fn
Try using PythonTex with Texpad.
[Python] Try using Tkinter's canvas
Try using Jupyter's Docker image
Try using scikit-learn (1) --K-means clustering
Try using matplotlib with PyCharm
Try using Azure Logic Apps
Try using Kubernetes Client -Python-
Try using the Twitter API
Try using OpenCV on Windows
[Django] Notes on using django-debug-toolbar
Try using Jupyter Notebook dynamically
Try using AWS SageMaker Studio
Try tweeting automatically using Selenium.
DEBUG settings when using Django
Try using SQLAlchemy + MySQL (Part 1)
Try using the Twitter API
Try using SQLAlchemy + MySQL (Part 2)
Try using the PeeringDB 2.0 API
Try using Pelican's draft feature
Try using pytest-Overview and Samples-
Try using folium with anaconda
Try using Janus gateway's Admin API
WEB application development using Django [Django startup]
Try using Spyder included in Anaconda
Try using design patterns (exporter edition)
Try using Pillow on iPython (Part 1)
WEB application development using Django [Application addition]
Try using Pillow on iPython (Part 2)
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using pynag to configure Nagios
Try using PyCharm's remote debugging feature
Django
Try using ArUco on Raspberry Pi
Try Ajax on the Django page
Try using cheap LiDAR (Camsense X1)
[Sakura rental server] Try using flask.
Reinforcement learning 8 Try using Chainer UI
Try to get statistics using e-Stat
Try using Python argparse's action API