0

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 ?

miyou995
  • 77
  • 10
  • Can you show how yiu render the items in the template? – Willem Van Onsem Oct 11 '21 at 16:39
  • Your `price` option also starts with two `<<`s. – Willem Van Onsem Oct 11 '21 at 16:47
  • 1
    Do you want a request to be made automatically on changing the select tag? Write some JavaScript to do that for you (Why do it though? Just let the user select their choice and make up their mind and press a button to actually filter). Here's a tip: Since you are already using it django-filter has an [OrderingFilter](https://django-filter.readthedocs.io/en/stable/ref/filters.html#orderingfilter). – Abdul Aziz Barkat Oct 11 '21 at 16:55
  • @WillemVanOnsem sure i edited the question and put the entire products template , thank you for you help – miyou995 Oct 12 '21 at 07:28
  • @AbdulAzizBarkat i did submit the form with some js with onchange="this.form.submit()" but it reload the page it doesn't call sorted_htmx_products function view, thank you for OrderingFilter i'm gona give it a try – miyou995 Oct 12 '21 at 07:30

1 Answers1

1

You use hx-trigger="change">.

AFAIK it needs to be changed with "d" at the end.

BTW: Please try to provide a minimal example the next time.

guettli
  • 25,042
  • 81
  • 346
  • 663