[英]how to sort a queryset using django and htmx?
我正在使用 django-filter 來應用過濾器,並且在新的查詢集上我想根據選擇標記更改新的查詢集排序進行 htmx 請求排序,這是我的觀點:
視圖.py
def sorted_htmx_products(request):
context = {}
qs= request.GET['order_by']
print('request', qs['order_by'])
if qs == "name_a":
querySet = Product.objects.all().order_by('name')
elif qs == "name_z":
querySet = Product.objects.all().order_by('-name')
elif qs == "price":
querySet = Product.objects.all().order_by('price')
elif qs == "brand":
querySet = Product.objects.all().order_by('brand')
else:
querySet = Product.objects.all()
products = ProductFilter(request.GET, queryset=querySet )
print('sorted products', products.qs)
context['products'] = products.qs
return render(request, 'snippets/htmx_products.html', context)
這是我提出 htmx 請求的 html 片段
<div class="product-select-box">
<div class="product-short" >
<form hx-get="{% url 'core:sorted-products' %}" hx-target="#removed-products" hx-swap="outerHTML" hx-trigger="change">
<p>Trier par:</p>
<select name="order_by" class="nice-select" >
<option value="default">Default</option>
<option value="name_a">Nom (A - Z)</option>
<option value="name_z">Nom (Z - A)</option>
<option value="price">Prix</option>
<option value="brand">Marque</option>
</select>
</form>
</div>
</div>
為什么這根本不起作用? 如何在選擇選項更改時進行簡單的 htmx 表單調用?
您使用hx-trigger="change">
。
AFAIK 它需要在末尾更改為“d”。
順便說一句:下次請嘗試提供一個最小的例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.