[PYTHON] What is a dog? POST Sending Volume Using Django--forms.py

Try using the Form class

Domo! This is Shiba Inu Ponta. When I see the train while taking a walk, I want to chase after it and start running, but since there is a lead, I can turn around and look around on the spot. In such a case, if you turn in the opposite direction, it will be cured, so everyone should try it! one!

Create forms.py

Well, today I'll create forms.py and modify it to define the Forms class.

wan/forms.py


from django import forms

class WanForm(forms.Form):
    dogname = forms.CharField(label='name')
    email = forms.EmailField(label='email')
    age = forms.IntegerField(label='age')
    message = forms.CharField(label='message')

Modify views.py

Then rewrite views.py as follows:

wan/views.py


from django.shortcuts import render
from .forms import WanForm

def index(request):
    params = {
        'title': 'Wan/Index',
        'message': "What's your message?",
        'form': WanForm(),
    }
    
    if request.method == "POST":
        params['message'] = request.POST['message']
        params['dogname'] = request.POST['dogname']
        params['email'] = request.POST['email']
        params['age'] = request.POST['age']
        params['form'] = WanForm(request.POST)

    return render(request, 'wan/index.html', params)

Modify urls.py

urls.py is simple.

wan/urls.py


from django.urls import path
from . import views

app_name = 'wan'

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

Change template

wan/templates/wan/index.html


<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>{{ title }}</title>
</head>
<body>
	<h1>{{ title }}</h1>
	<h2>received data</h2>
	<table>
		<tr>
			<th>name:</th><td>{{ dogname }}</td>
		</tr>
		<tr>
			<th>email:</th><td>{{ email }}</td>
		</tr>
		<tr>
			<th>age:</th><td>{{ age }}</td>
		</tr>
		<tr>
			<th>message:</th><td>{{ message }}</td>
		</tr>
	</table>

	<h2>input form</h2>
	<form action="{% url 'wan:index' %}" method="POST">
		{% csrf_token %}
		<table>
			{{ form.as_table }}
			<tr>
				<th></th>
				<td>
				<input type="reset" value="cancel">
				<input type="submit" value="click">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

Display test

I feel like this. スクリーンショット 2020-08-29 23.29.20.png

Thank you for your hard work. Good night one!

Recommended Posts

What is a dog? POST Sending Volume Using Django--forms.py
What is a dog? Django installation volume
What is a dog? Python installation volume
What is a dog? Challenge Django templates! Volume
What is a dog? Volume of GET request and query parameters
What is a dog? Django--Get Name and Date from URL Volume
What is a dog? Django--Getting Started with Form for the First Time POST Transmission Volume
What is a dog? Django--Create a custom user model
What is a dog? Django--Create a custom user model 2
What is a distribution?
What is a terminal?
What is a hacker?
What is a pointer?
What is a dog? Django App Creation Start Volume--startapp
What is a dog? Django App Creation Start Volume--startproject
What is a dog? Django--Volume of using values obtained from URLs in class-based views
What is a decision tree?
What is a Context Switch?
What is a super user?
What is a system call
[Definition] What is a framework?
What is a callback function?
What is a python map?
[Python] What is a zip function?
[Python] What is a with statement?
What is a lexical scope / dynamic scope?
What is a Convolutional Neural Network?
It's a Mac. What is the Linux command Linux?
Tell me what a conformal map is, Python!