2013-10-10 96 views
1

我在django和SQL一起創建了一家巧克力店。如何在使用django的商店中創建「產品過濾器」?

目前,我可以連接到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;[email protected];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})) 

這是過濾器菜單我已經在巧克力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這樣做。

非常感謝提前!

回答

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

+0

通讀你是對的,但我想她正在使用django不支持的數據庫。無論如何,她可以使用[django-pyodbc](https://github.com/lionheart/django-pyodbc/)。您可能還想包含一些指向[django表單文檔]的鏈接(https://docs.djangoproject.com/zh/dev/ref/forms/)。 – esauro

相關問題