【发布时间】:2023-03-28 01:29:01
【问题描述】:
我是 django 的初学者。而且,我无法查看用户选择的选项的值。我必须在那里应用一些逻辑。
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>
这些选项与模型无关。 所以现在,当我选择低价并按下按钮时,我得到了这个:(在 django 控制台中)
[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
如您所见,“过滤器”正在打印。但我想要的是特色的价值,比如 pricelow 或 pricehigh。
【问题讨论】:
标签: django django-forms django-views django-templates