1

I am at beginner level in django. And, I am unable to get the value of user selected option in view. I have to apply some logic there.

views.py

def shop(request):

    if request.GET.get('featured'):
        featured_filter = request.GET.get('featured')
        print(featured_filter) #debugging purpose
    else:
        print("\n\nnone\n\n") #debugging purpose

    bookz = Book.objects.order_by('title')
    var = {'books': bookz, 'range': 10}
    return render(request, 'bookrepo/shop.html', context=var)

shop.html

<form action="{% url 'bookrepo:shop' %}" method="GET">
    <select name="featured" class="custom-select-lg custom-select">
        <option selected><h1>Filter</h1></option>
        <option value="pricelow">Low price</option>
        <option value="pricehigh">High price</option>
        <input type="submit" name="featured" value="Filter" />
    </select>
</form>

These option things have nothing to do with models. So right now, I am getting this when I select low price and press button: (in django console)

[10/May/2020 00:18:20] "GET /shop/?featured=pricehigh&featured=Filter HTTP/1.1" 200 47486
[10/May/2020 00:18:20] "GET /static/js/js.dom.changer.js HTTP/1.1" 304 0
Filter
[10/May/2020 00:18:24] "GET /shop/?featured=pricelow&featured=Filter HTTP/1.1" 200 47486

As u can see "Filter" is getting printed. But i want is featured's value like pricelow or pricehigh.

Yagami
  • 305
  • 2
  • 14

1 Answers1

1

The reason this happens is because your request has two values for featured: pricehigh and Filter, indeed:

/shop/?featured=pricehigh&featured=Filter

the second one is caused by the submit button. You can simply remove the name="…" attribute from the button:

<form action="{% url 'bookrepo:shop' %}" method="GET">
    <select name="featured" class="custom-select-lg custom-select">
        <option selected><h1>Filter</h1></option>
        <option value="pricelow">Low price</option>
        <option value="pricehigh">High price</option>
        <input type="submit" value="Filter" />
    </select>
</form>
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thanks. It worked. I got your point to remove name attribute from button. In my case it is name="featured" not name="hellip;". So did i get your point right? – Yagami May 09 '20 at 20:33
  • @Sayyam: the `hellip` was a misrendered ellipsis :) to refer to the attribute part :) – Willem Van Onsem May 09 '20 at 20:35
  • Its fine :) Is there a way to get value of selected "option" without submit button ? – Yagami May 09 '20 at 20:39
  • @Sayyam: well you submit the form, and then the view aims to read the data. You can use an AJAX call to submit for example a form without visiting a new page: https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html – Willem Van Onsem May 09 '20 at 20:43
  • I know i am being sticky. But can you answer this [link](https://stackoverflow.com/questions/61692157/django-3-0-reverse-for-product-with-no-arguments-not-found-1-patterns-trie) too? You seem very pro in django. I don't know a thing about slugs and I really need an answer to my problem. Thanks :) – Yagami May 09 '20 at 21:11