2015-09-10 65 views
0

我試圖獲得通過從請求Django獲取的類別ID過濾的項目。它看起來像這樣:來自url的多個請求filtr Djjango

url(r'^productlist.aspx&category(?P<category>\d*)$', 'shop.views.shoplist'), 

所以URL看起來像:

http://example.com/store/productlist.aspx&category6 

其中 「6」 是類別ID

這裏是views.py

def shoplist(request, category, format=None): 
    args = {} 
    args.update(csrf(request)) 
    args['products'] = ShopProduct.objects.all().order_by('-id') 
    if category: 
     args['products'] = ShopProduct.objects.filter(shop_product_category_id = category) 
    return render_to_response('shop-catlist.html', args) 

它工作的很好,但niw我想過濾產品的多個類別ID的

的URL會看起來像這樣:http://example.com/store/store.aspx&category1,2,3,4,5

請幫我

注:ID的將通過在形式上覆選框被收集的類別,URL將產生根據用JavaScript

選擇複選框

回答

1

不要像這樣格式化URL。有路過的查詢URL中的一個完全合適的方式,那就是用一個查詢字符串:

http://example.com/store/productlist/?category=1&category=2&category=3 

(?另外,爲什麼你假裝你的Django網站上.NET運行時,不要做到這一點)

現在您的網址模式,就像是這樣的:

url(r'^productlist/$', 'shop.views.shoplist'), 

,你的看法是:

def shoplist(request): 
    categories = request.GET.getlist('category') 
    if categories: 
     products = ShopProduct.objects.filter(shop_product_category_id__in=categories) 
+0

嗯,是療法以任何方式通過「?」或者用一些東西代替?由於我提供的網址格式是簡化的網址格式,因此它原來的格式如下所示:url(r'^ productlist.aspx&shop(?P \ d *)&category(?P \ d *)&subcategory(?P \ d *)&discount(?P \ d *)$','shop.views.shoplist'),' –

+0

這正是您不應該在URL中做這些的原因,而是作爲查詢參數。 –

+0

'example.com/store/productlist/?category=1&category=2&category=3?subcategory=1&subcategory=4&subcategory=13'可以顯示不同的參數查詢嗎? –