我正在使用 Django 为商店创建过滤器菜单。当一个复选框被选中时,用户提交一个 GET 表单并且站点重定向到相同的 url,但带有新的产品结果。
过滤器显示过滤后的产品,但之前选中的复选框现在未选中。我想保留那些选中的复选框。我怎么能这样做?
这是我的巧克力菜单.html:
<form action = '/chocolate-menu' method ='GET' >
<table width = "595px" class = "center_filter" style ="border-collapse:collapse;table-layout:fixed">
<tr>
<td width = "110px" class = "no_border"> <span style = "color:9A8478;font-family: Helvetica Neue, Arial;font-weight: bolder; font-size:20px">Filter by</span> </td>
<td width = "119px"> <span style = "color:C39A6B; font-weight: bold; font-size:18px">Price </span></td>
<td width = "119px"> <span style = "color:C39A6B; font-weight: bold; font-size:18px">Flavour </span></td>
<td width = "128px"> <span style = "color:C39A6B; font-weight: bold; font-size:18px">Special Diet </span></td>
<td width = "119px"> <span style = "color:C39A6B; font-weight: bold; font-size:18px">Calories </span></td>
</tr>
<tr>
<td class = "no_border"></td>
<td> <input type="checkbox" style="margin-right: 10px;" id="five_to_ten" value='five_to_ten' name='five_to_ten' >£5 - £10</td>
<td> <input type="checkbox" style="margin-right: 10px;" id="dark" value = "dark" name = "dark">Dark </td>
<td> <input type="checkbox" style="margin-right: 10px;" id="lactose_free" value='lactose_free' name = "lactose-free">Lactose-free </td>
<td> <input type="checkbox" style="margin-right: 10px;" id="zero_to_hundret" value = 'zero_to_hundret' name='zero_to_hundret'> 0 - 100 </td>
</tr></table></form>
这是我在 views.py 中的巧克力菜单功能
def showChocoMenu(request):
connection = pyodbc.connect('Driver={SQL Server Native Client 11.0};Server=xx;Database=xx;Uid=xx;Pwd=xx;Encrypt=yes;Connection Timeout=30;')
cur = connection.cursor()
filter = "this filter gets a string that complements the SQL query later"
if (filter == ''):
cur.execute("SELECT distinct choco_name, choco_price FROM chocolates c, stock s WHERE c.choco_ID=s.choco_ID AND s.availability > 0 AND s.country ='UK'")
chocolateMenu = cur.fetchall()
cur.close()
connection.close()
else:
cur.execute("SELECT distinct choco_name, choco_price FROM chocolates c, stock s WHERE c.choco_ID=s.choco_ID AND s.availability > 0 AND s.country ='UK' AND " + filter )
chocolateMenu = cur.fetchall()
cur.close()
connection.close()
return HttpResponse(render_to_string('chocolate_menu.html',{'chocolateMenu':chocolateMenu}))