如何创建产品过滤器'在一家使用django的商店?

时间:2013-10-10 10:39:49

标签: python django filtering shop

我正在与django一起创建一个巧克力店。

目前,我可以连接到SQL db并在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>

我想我可以使用包含方法POST的表单来调用过滤器函数,但我不知道如何在python中这样做。

非常感谢提前!

1 个答案:

答案 0 :(得分:2)

Django有an ORM可以用来避免编写纯SQL。这样可以更轻松地与您的模型进行交互。所以对于像这样的模型:

class Chocolate(model.Model):
     availability = models.IntegerField(...)
     country = models.CharField(...)
     ...

您可以进行返回QuerySet个对象的查询(从查询返回的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')

您应该仔细阅读上面提到的文档以熟悉一般的查询,然后查看different queries you can achieve with the orm,并阅读Django tutorial