2016-03-03 71 views
0

我試圖在ListView上做一個價格過濾器。在jquery ui滑塊中停止事件時,過濾器將作爲ajax滑塊傳遞。爲了使結果重寫get_queryset()函數。奇怪的是get_queryset()不會在ajax請求後給出新的結果,即使get_queryset()中的IF語句執行,因爲我可以在控制檯中打印價格變量。我究竟做錯了什麼 ? :SDjango的JQuery UI滑塊通過Ajax過濾

AJAX請求仍然給出的產品爲Product.objects.all()結果,而不是Product.objects.filter(價格= 0)

伊夫還試圖把所屬類別成get_context_data()作爲上下文['procuct_list'],因此它會覆蓋get_queryset()的結果,但它不起作用

!UPDATE! 該網址似乎過濾時,我通過地址欄發送獲取請求,但它只是不通過ajax調用?

它似乎不工作只是與Ajax爲什麼?

類ShopView

class ShopView(ListView): 
    model = Product 
    template_name = "shop/shop.html" 
    context_object_name = "product_list" 

def get_context_data(self, **kwargs): 
    context = super(ShopView, self).get_context_data(**kwargs) 
    context['category_list'] = Category.objects.all() 
    return context 

def get_queryset(self): 
    price = self.request.GET.get('price') 
    if price: 
     print price #This gets printed 
     return Product.objects.filter(price=0) #But this fails!? 
    else: 
     return Product.objects.all() 

了jQuery UI滑塊

$(function() { 
    var priceSlider = ".price-slider"; 
    var priceMin = "span.min-price"; 
    var priceMax = "span.max-price"; 
    $(priceSlider).slider({ 
     range: "min", 
     value:5, 
     min: 1, 
     max: 5, 
     slide: function(event, ui) { 
      $(priceMax).html(ui.value + "€"); 
     }, 
     stop: function(event, ui) { 
      var price = ui.value; 
      $.ajax({ 
       type: "GET", 
       data: "price=" + price, 
       cache: false, 
      }); 
     } 
    }); 

的爲週期進行渲染產品

{% for product in product_list %} 
    {% include "shop/product.html" %} 
{% endfor %} 

產品本身的HTML

<div class="product-price"> 
    <p>{{ product.price }}€</p> 
</div> 
...etc 

回答

0

沒關係,愚蠢的錯誤,我以後沒有做任何與AJAX請求。

success: function(data) { 
    $("body").html(data); 
}