简体   繁体   English

使用下拉列表过滤项目

[英]Filtering items using drop down list

Would like to filter features(products) by using dropdown menu. 想使用下拉菜单过滤功能(产品)。 Each of the feature(product) got a tag (eg food, drink, random). 每个特征(产品)都有一个标签(例如食物,饮料,随机)。 Idea is when user selects the tag on a menu, it shows only those items who's got that tag. 想法是当用户在菜单上选择标签时,它仅显示那些具有该标签的项目。 So far I went as far, but doesn't seem to work yet. 到目前为止,我走了很远,但似乎还没有工作。 PyCharm does not give an error, but not functioning. PyCharm没有出错,但没有发挥作用。 What I am missing? 我错过了什么? Thank you! 谢谢!

my models.py 我的models.py

class Feature(models.Model):
FOOD = 'food'
DRINK = 'drink'
RANDOM = 'random'
TAGS = (
    (FOOD, 'food'),
    (DRINK, 'drink'),
    (RANDOM, 'random')
)
name = models.CharField(max_length=40, default='')
tags = models.CharField(max_length=20, choices=TAGS, default=ALL)

def __str__(self):
    return self.name

my views.py 我的views.py

def tags(request):
if request.GET.get('tags'):
    features_filter = request.GET.get('tags')
    listings = Feature.objects.filter(features_filter=features_filter)
else:
    listings = Feature.objects.all()

context = {'listings': listings}
return render(request, 'index', context)

my index.html 我的index.html

<form action="{% url 'index' %}" method="get" accept-charset="utf-8">
{% csrf_token %}
 <select name="tags">
     {% for feat in features %}
        <option value="{{feat.tags}}">{{ feat.tags }}</option>
     {% endfor %}
 </select>
<input type="submit" value="submit">
</form>


{% for feature in features %}            
  <h1{{ feature.name }}</strong></h1>
{% endfor %}

Try changing this 尝试改变这个

 listings = Feature.objects.filter(features_filter=features_filter) 

to

listings = Feature.objects.filter(tags=features_filter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM