Python 如何创建';产品过滤器&x27;在使用django的商店里?

Python 如何创建';产品过滤器&x27;在使用django的商店里?,python,django,filtering,shop,Python,Django,Filtering,Shop,我正在django与SQL一起创建一家巧克力店 目前,我可以连接到SQL数据库,并在我的chocolate-menu.html文件中显示所有现有的巧克力。我想集成一个过滤菜单,这样人们可以根据价格、口味等过滤巧克力 我在views.py中使用以下函数显示所有巧克力: def showChocoMenu(request): connection = pyodbc.connect('Driver={SQL Server Native Client 11.0};Server=tcp:xxxx.

我正在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' >&pound;5 - &pound;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' >&pound;10 - &pound;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>

过滤
价格
风味
特殊饮食
卡路里
&磅;5英镑;10
黑暗的
无乳糖
0 - 100 
&磅;10英镑;20
白色
无糖
100 - 250 
我想我可以使用include表单和方法POST来调用过滤器函数,但我不知道如何在python中这样做

非常感谢

Django提供了一个可以用来避免编写普通SQL的工具。这使得与模型的交互更加容易。对于这样的模型:

class Chocolate(model.Model):
     availability = models.IntegerField(...)
     country = models.CharField(...)
     ...
您可以进行返回
QuerySet
对象的查询(从查询返回的django模型实例的分组):


您应该通读上面提到的文档以熟悉一般的查询,然后查看,并通读

您是对的,但我猜她使用的数据库不是django特别支持的数据库。反正她可以用。您可能还需要包含一些指向的链接。
chocolates = Chocolate.objects.all() # This will get all chocolates
chocolates = Chocolate.objects.filter(availability__gt = 0)
chocolates = Chocolate.objects.filter(country='UK')
chocolates = Chocolate.objects.filter(availability__gt = 0, country='UK')