我一直在尝试创建用户界面,从数据库中筛选出结果。重要的是,我希望过滤器是'加法'。所以,如果用户选择一个过滤器,页面将重定向并显示结果。之后,用户可以选择另一个过滤器,结果将缩小到两个过滤器。对于任何数量的过滤器,都应继续此操作。你知道吗
这就是现在的样子
@app.route('/')
def home():
kind = request.args.get('kind')
price = request.args.get('price')
category = request.args.get('category')
filters = {}
if price is not None: filters['params.price'] = {'$lt' : int(price) }
if kind is not None: filters['kind'] = kind
if category is not None: filters['category'] = category
posts = db.collection.find(filters)
return render_template('home.html', posts=posts)
我的hrefs使用jinja2模板的链接看起来像
<li><a href="{{ url_for ('home', kind='m') }}">Label</a></<li>
<li><a href="{{ url_for ('home', price=50000)}}">Label</a></li>
<li><a href="{{ url_for ('home', category='p') }}">Label</a></li>
... many more similar links
当前,这将作为URL的覆盖。如果我点击其中任何一个链接,它只会替换整个URL并使用链接中的变量。你知道吗
first link: http://127.0.0.1/?kind=m
second link: http://127.0.0.1/?price=5000
third link: http://127.0.0.1/?category=p
我希望它做的是附加查询-如果我单击任何链接,它会记住以前选择的过滤器并“添加”上次单击的链接。下面我将展示我期望它如何工作。你知道吗
first link: http://127.0.0.1/?kind=m
second link: http://127.0.0.1/?kind=m?price=50000
second link: http://127.0.0.1/?kind=m?price=50000?category=p
您可以将所有筛选器值(
None
最初)传递给视图,并将它们作为参数添加到url_for
调用中。链接中将不包括None
过滤器。你知道吗相关问题 更多 >
编程相关推荐