2015-01-13 45 views
0

我想設置一個小的高級搜索,允許通過代碼,國家,標題搜索產品。在我的views.py我:使用表單與Flask進行「高級搜索」的正確方法?

@app.route('/advanced_search_results/<code>/<country>/<title>', methods=['GET', 'POST']) 
def advanced_search_results(code, country, title): 
    products = models.Product.query.filter_by(code=code, country=country, title=title).all() 
    return render_template('advanced_search_results.html', products=products) 


@app.route('/advanced_search', methods=['GET', 'POST']) 
def advanced_search(): 
    form = AdvancedSearchForm() 
    if form.validate_on_submit(): 
     code = form.code.data 
     country = form.country.data 
     title = form.title.data 
     return redirect(url_for('advanced_search_results', code=code, country=country, title=title)) 
    return render_template('advanced_search.html', title='Advanced Search', form=form) 

有一些明顯的問題在這裏,但大多數關注我的是一種先進的搜索將在URL落得像www.mysite.com/advanced_search_results/the_code/the_country/ the_title,這個字符串只會隨着搜索的進一步升級而變長。我是朝着正確的方向走,還是錯過了一個更好的處理方式?

回答

1

你可以使用查詢字符串 - 即你的URL看起來像:

mysite.com/advanced_search_results?country=USA &碼= 42 &標題= aTitle

from flask import request 
@app.route('/advanced_search_results') 
def adv_search_results(): 
    country = request.args.get('country') 
    code = request.args.get('code') 
    title = request.args.get('title') 
    products = models.Product.query.filter_by(code=code, country=country, title=title).all() 
    return render_template('advanced_search_results.html', products=products) 

而且請注意0​​