我正在用 django 和 SQL 一起创建一家巧克力店。
目前,我可以连接到 SQL 数据库并在我的 Chocolate-menu.html 文件中显示所有现有的巧克力。我想集成一个过滤菜单,以便人们可以按价格、口味等过滤巧克力。
我在 views.py 中使用以下函数显示所有巧克力:
def showChocoMenu(request):
connection = pyodbc.connect('Driver={SQL Server Native Client 11.0};Server=tcp:xxxx.database.windows.net,1433;Database=xxx;Uid=xxxx@xx;Pwd=xxx;Encrypt=yes;Connection Timeout=30;')
cur = connection.cursor()
cur.execute("SELECT 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()
return HttpResponse(render_to_string('chocolate_menu.html',{'chocolateMenu':chocolateMenu}))
这是我在 Chocolate-menu.html 文件中创建的过滤器菜单。
<table width = "50%" class = "center_filter" style ="border-collapse:collapse;">
<tr>
<td width = "10%" class = "no_border"> <span style = "color:9A8478;font-family: Helvetica Neue, Arial;font-weight: bolder; font-size:20px">Filter by</span> </td>
<td width = "10%"> <span style = "color:C39A6B; font-weight: bold; font-size:18px">Price </span></td>
<td width = "10%"> <span style = "color:C39A6B; font-weight: bold; font-size:18px">Flavour </span></td>
<td width = "10%"> <span style = "color:C39A6B; font-weight: bold; font-size:18px">Special Diet </span></td>
<td width = "10%"> <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' >£5 - £10</td>
<td> <input type="checkbox" style="margin-right: 10px;" id="dark" value = 'dark'>Dark </td>
<td> <input type="checkbox" style="margin-right: 10px;" id="lactose_free" value='lactose_free'>Lactose-free </td>
<td> <input type="checkbox" style="margin-right: 10px;" id="zero_to_hundret" value = 'zero_to_hundret'> 0 - 100 </td>
</tr>
<td class = "no_border"></td>
<td> <input type="checkbox" style="margin-right: 10px" id="ten_to_twenty" value ='ten_to_twenty' >£10 - £20</td>
<td> <input type="checkbox" style="margin-right: 10px" id="white" value = 'white'>White </td>
<td> <input type="checkbox" style="margin-right: 10px" id="sugar_free" value = 'sugar_free'>Sugar-free </td>
<td> <input type="checkbox" style="margin-right: 10px" id="hundret_to_twofifty" value='hundret_to_twofifty'>100 - 250 </td>
</tr>
</table>
我想我可以使用包含一个带有方法 POST 的表单来调用过滤器函数,但我不知道如何在 python 中这样做。
提前谢谢了!