python - 无法将参数从模板传递到 Django 视图
问题描述
我将三个参数从我的 html 模板传递给 django view.py。但是每当我点击提交时,它就会显示给我Page not found
,。我还是编程和 Django 的新手。有谁知道我该如何解决这个问题?预期的结果是网页能够显示用户的所有输入。结果应显示在同一页面上,而不是其他页面上。
我的模板
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>Alert Policies</h2>
</div>
<div class="modal-body">
<p style="font-size:14px">Please select an event parameter as well as the condition type and value that apply.</p>
<!-- parameter drop down -->
<form action="send" method="post">
<label for="Parameter"> <b style="font-size:13px" > Event parameter to evaluate </b></label>
<select name="Parameter" id="Parameter" style="width:340px; font-family: 'Quicksand', sans-serif;">
<option disabled selected value>select a parameter</option>
<option value="Subject">Subject</option>
<option value="Text">Text</option>
</select>
<br><br>
<label for="Condition"> <b style="font-size:13px" > Type of condition </b></label>
<select name="Condition" id="Condition" style="width:340px; margin-left:69px; font-family: 'Quicksand', sans-serif;">
<option disabled selected value>select a condition</option>
<option value="Equals">Equals</option>
<option value="Contain">Contain</option>
<option value="NotContain">Does not contain</option>
</select>
<br><br>
<label for="valuetomatch"> <b style="font-size:13px" > Value to match</b></label>
<input type="text" id="valuetomatch" name="valuetomatch" style="width:333px; margin-left:80px; font-family: 'Quicksand', sans-serif;">
<br>
<br>
<button class="button"><span>OK</span></button>
</form>
{{key}}
</div>
我的观点.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request,'DemoApp/hi.html')
def send(request):
a=str(request.POST["Parameter"])
b=str(request.POST["Condition"])
c=str(request.POST["valuetomatch"])
haha = a+b+c
return render(request,'DemoApp/hi.html',{'key':haha})
# Create your views here.
我的网址
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.index,name='home-page'),
path('', views.send, name='test'),
]
解决方案
您的表单有<form action="send" method="post">
,但您没有任何网址/send
。因此,您可以像这样更改 urls.py 文件:
urlpatterns = [
path('', views.index,name='home-page'),
path('send/', views.send, name='test'),
]
您应该考虑在模板中使用命名的 url(通过url 模板标签),如下所示:
<form action="{% url 'test' %}" method="post">
推荐阅读
- r - RO OOP 改变一个槽的名字
- c++ - 由于使用 If-then 案例而提高了性能
- android - Android InputManagerCompat.InputDeviceListener 自定义类没有收到任何方法的回调
- haskell - Haskell - 自定义类型的元组
- python-3.x - 通过字典在python中动态导入
- php - Laravel 5 - 如何在 3 个表之间建立关系?
- python - 如何在不显示渐变的情况下打印张量
- python - 按顺序指定数量的 1 和 0 的 Numpy 数组
- php - 使用 guzzle 的连接池策略
- c# - 如何使用 .NET 折线图覆盖多个相同功能的图,而无需 .NET 在所有点之间绘制线