Djangoのテンプレートでつかえるas_tableを使って、
フォーム入力画面を作ります。
テンプレート index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <body> {% with test_form as form %} <form action="{% url 'testapp:result' %}" method="post"> {% csrf_token %} <table> {{ form.as_table}} </table> <input type="submit" value="送信"> </form> {% endwith %} </body> </html> |
{{ form.as_table}} で、フォームクラスの値に<label>, <tr>, <th>, <td>をつけて、
表形式にしています。
フォームクラス forms.py
1 2 3 4 5 |
from django import forms class testForm(forms.Form): id = forms.IntegerField(label='ID') name = forms.CharField(label='名前') |
ビュー views.py
1 2 3 4 5 6 7 8 |
from django.http import HttpResponse from django.shortcuts import render from testapp.forms import testForm def index(request): test_form = testForm() return render(request, 'index.html', {'test_form':test_form}) |
実行結果