1

我正在用 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>

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

提前谢谢了!

4

1 回答 1

3

Django 有一个 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')

您应该通读上面提到的文档以熟悉一般的查询,然后查看您可以使用 orm 实现的不同查询,并阅读Django 教程

于 2013-10-10T11:04:36.900 回答