1

i'm using django-filter to apply filters, and on the new queryset i want to make an htmx request sort that depend on select tag change the new queryset sorting, here is my view:

views.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)

and here is my html snippet where i made the htmx request

            <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>
        

why this doesn't work at all ? how to make a simple htmx form call on select option change ?

4

1 回答 1

0

你用hx-trigger="change">.

AFAIK 最后需要用“d”更改

顺便说一句:下次请尝试提供一个最小的示例。

于 2021-10-12T19:46:56.293 回答