我正在做一个类似于高级过滤搜索的东西,在烧瓶中,我有如下输入:
<form action="/search" method="post">
<div class="form-group">
<input type="text" class="form-control" id="letter" name="numbersearch" placeholder="Enter car number">
</div>
<div class="form-group">
<select class="form-control" id="Select1" name="brandsearch">
<option value="">select brand</option>
<option value="A">AUDI</option>
<option value="B">BMW</option>
</select>
</div>
<div class="form-group">
<select class="form-control" id="Select2" name="typesearch">
<option value="">select type</option>
<option value="Au">Automatic</option>
<option value="Mn">Manual</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" id="word" name="carsearch" placeholder="Enter automotive name">
</div>
</form>
我一直以一种方式写作:
mod_automobile = Blueprint('dictionary', __name__)
@mod_automobile .route('/')
def index():
return render_template('index.html')
@mod_automobile .route('/search', methods=['GET', 'POST'])
def search():
numbersearch=request.args.get('numbersearch') or None
brandsearch=request.args.get('brandsearch') or None
typesearch=request.args.get('typesearch') or None
carsearch=request.args.get('carsearch') or None
''' Here I am writing conditions for single/multiple combination selection's and search database'''
if numbersearch and brandsearch: '''If user selects only these two'''
'''.............'''
return render_template('sometemplate1.html')'''new template'''
elif numbersearch: '''If user selects only numbersearch'''
'''...............'''
return render_template('sometemplate2.html')'''a new template'''
elif brandsearch:
'''....'''
....'''similarly all possible conditions''''
在这里,我有有限的条件,所以我可以设法做到这一点,但我担心,如果这样的条件很多,如何管理它们呢
在单路由方法下编写所有条件也很好吗
我正在使用mongodb,每个选中的条件都有自己的集合或多个集合要搜索,因此我无法在那里编写动态查询()
我正在搜索汽车名称,比如“porsche”,我将收藏名称维护为a_collection
(对于以a
信息开头的所有汽车),类似地b
,c
,z
为此,它符合上述条件*
elif carsearch:
collection_name=word[0].lower()+'_collection'
cars=db[collection_name]
data=cars.find({'car':carsearch})
return render_template('carsearch.html',data=data)
所有集合中相同的示例文档格式(p\ U集合):
{
"_id": {
"$oid": "5e0c70f22b15fc7b3b218a94"
},
"car": "Porsche",
"carset_id": "76c6ebfae9",
"details": [{
"id": "4c21c72afa",
"engine_model": "flat-six"
}]
}
寻找一个关于如何管理在一个更有意义的格式这方面的建议,任何帮助是非常感谢
您可以使用字典来管理许多可能的条件。 将搜索选择参数放入一个元组中,例如:
然后,您就有了一个字典,其中填充了作为键的选择元组和作为值执行的函数
示例:
从本词典中获取必要的函数:
现在调用所选函数:
完成
注意:通过这种方式,您可以在代码中的其他地方定义函数,还可以避免许多if/elif条件
相关问题 更多 >
编程相关推荐