このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
50_dialy:2023:06:03 [2023/06/03 07:34] – matsui | 50_dialy:2023:06:03 [2023/06/03 07:54] (現在) – matsui | ||
---|---|---|---|
行 64: | 行 64: | ||
] | ] | ||
</ | </ | ||
+ | |||
+ | ===== 3.urls.py修正 ===== | ||
+ | |||
+ | 作成したtestappアプリのviewへアクセスするURL Pathを作成 | ||
+ | < | ||
+ | import testapp.views as test_view | ||
+ | |||
+ | urlpatterns = [ | ||
+ | path(' | ||
+ | ] | ||
+ | </ | ||
+ | |||
+ | ===== 4.view.pyを修正 ===== | ||
+ | |||
+ | GET されたら、ただテンプレートを表示。 | ||
+ | POST されたら、POSTされた値を表示するだけの、view | ||
+ | |||
+ | < | ||
+ | from django.shortcuts import render | ||
+ | |||
+ | # Create your views here. | ||
+ | from django.shortcuts import render, redirect, get_object_or_404 | ||
+ | from django.views.generic import TemplateView | ||
+ | |||
+ | from testapp.models import * | ||
+ | import logging | ||
+ | logging.basicConfig(level=logging.INFO) | ||
+ | logger = logging.getLogger(__name__) | ||
+ | |||
+ | class View(TemplateView): | ||
+ | template_name = " | ||
+ | |||
+ | def get(self, request): | ||
+ | context = {} | ||
+ | return render(self.request, | ||
+ | def post(self, request): | ||
+ | context = { | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | logger.info(request.POST) | ||
+ | return render(self.request, | ||
+ | </ | ||
+ | |||
+ | ===== 5.template作成 ===== | ||
+ | |||
+ | 作成したアプリフォルダの下に、templatesフォルダを作成し、view.pyで記述したtest.htmlを用意 | ||
+ | < | ||
+ | Hello World! | ||
+ | <br> | ||
+ | |||
+ | {% if action %} | ||
+ | action = {{ action }} | ||
+ | <h1> | ||
+ | {{ action }} が押されました。 | ||
+ | </h1> | ||
+ | name1 = {{name1}}< | ||
+ | name2 = {{name2}} | ||
+ | {% endif %} | ||
+ | <form method=' | ||
+ | {% csrf_token %} | ||
+ | <input type=' | ||
+ | <input type=' | ||
+ | <button type=' | ||
+ | <button type=' | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 6.ページ確認 ===== | ||
+ | |||
+ | GET後 | ||
+ | {{: | ||
+ | |||
+ | POST後 | ||
+ | {{: | ||
{{tag> | {{tag> |