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!
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')
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)
urls.py is simple.
wan/urls.py
from django.urls import path
from . import views
app_name = 'wan'
urlpatterns = [
path('', views.index, name='index'),
]
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>
I feel like this.
Thank you for your hard work. Good night one!
Recommended Posts