django从HTML表单中打印复选框的值



我有一个HTML表单,它显示了一个包含数据的表。我在表单中添加了复选框,现在想迈出一小步来打印复选框的值,当按钮名为">"test_btn">在我的HTML表单上。但是,由于缺少控制台日志,我知道我没有进入get_checked_items函数。任何想法都将不胜感激。

这是我的HTML表单show.HTML

<body>
<div class="container">
<form method="POST" action=#>
{% csrf_token %}
<table class="table table-striped">
<thead>
<tr>
<th>Shipment ID</th>
<th>Load Number</th>
<th>Booked By</th>
<th>Pickup City</th>
<th>Pickup State</th>
<th>Pickup Date</th>
<th>Pickup Time</th>
<th>Destination City</th>
<th>Destination State</th>
<th>Trailer Number</th>
<th>Seal Number</th>
<th>Primary Driver</th>
</tr>
</thead>
<tbody>
{% for ship in shipment %}  
<tr>
<td><input type="checkbox" name="checks" id="{{ship.id}}" value="{{ship.id}}" />{{ship.id}}</td>
<td>{{ship.load_number}}</td>
<td>{{ship.booked_by}}</td>
<td>{{ship.pickup_city}}</td>
<td>{{ship.pickup_state}}</td>
<td>{{ship.pickup_date}}</td>
<td>{{ship.pickup_time}}</td>
<td>{{ship.destination_city}}</td>
<td>{{ship.destination_state}}</td>
<td>{{ship.trailer_number}}</td>
<td>{{ship.seal_number}}</td>
<td>{{ship.primary_driver}}</td>
</tr>
{% endfor %} 
</tbody>
</table>
<div class="form-group">
<button><a href="/showform">Enter New Shipment</a></button>
<div class="form-group">
<button><a href="/updatedata">Update Data</a></button>
</div>
<input type ="submit" class="btn" value="TESTING" name="test_btn">
</form>

这是我的views.py文件。我有几个打印语句,只是为了让我更清楚地了解我实际输入的函数。

from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models import Shipment
# Create your views here.
def show(request):
shipments = Shipment.objects.all()
print("I am at the show function in views.py")
return HttpResponse(render(request,"displaydata/show.html",{'shipment':shipments}))

def get_checked_items(request):
if (request.POST.get("test_btn")):
print("i made it into the get_checked_items function in views.py")
checked_item = request.POST.getlist("checks")
print(checked_item)
return HttpResponse(render(request,"displaydata/show.html"))

这是我为特定应用编写的URLS.py

from django.urls import path
from . import views
app_name = 'displaydata'
urlpatterns = [
path('show/', views.show, name='show'),
path('get_checked_items/', views.get_checked_items, name='get_checked_items')
]

更改操作属性以将数据发送到预期视图

<form method="POST" action="/get_checked_items/">

最新更新