Python 烧瓶url函数工作不正常
我有一个html表单,它通过POST发送数据,然后使用GET将表单数据重定向到同一个函数作为函数的参数Python 烧瓶url函数工作不正常,python,python-3.x,flask,Python,Python 3.x,Flask,我有一个html表单,它通过POST发送数据,然后使用GET将表单数据重定向到同一个函数作为函数的参数 @app.route('/search/custom/', methods=['GET', 'POST']) def search_custom(category=None, date=None, page=None): if request.method == 'POST': category = request.form.get('InputCategory')
@app.route('/search/custom/', methods=['GET', 'POST'])
def search_custom(category=None, date=None, page=None):
if request.method == 'POST':
category = request.form.get('InputCategory')
date = request.form.get('InputDate')
return redirect(url_for('search_custom', category=category, date=date, page=1))
if request.method == 'GET':
if not(category and date and page):
return redirect(url_for('home'))
flash('worked', 'success')
return redirect(url_for('register'))
函数正在正确接收参数,但其唯一重定向到主:
127.0.0.1 - - [10/Apr/2018 01:55:36] "GET /search/custom/?category=Vetements&date=Dernier+mois&page=1 HTTP/1.1" 302 -
处理get请求时,类别、日期和页面均为无 在Get处理程序内部,您需要实际从查询字符串中提取参数 比如:
category = request.args.get('category')
date = request.args.get('date')
page = request.args.get('page')
我们应该做到这一点
在检查参数之前,请先检查,它应该可以工作
我没有机会测试这个,所以如果它不起作用,请告诉我,我会更深入地研究它
按照现在的方式,您可能希望为其创建一个格式化的url。类似于/search/custom//。不过,这也需要更改传入url的格式,而这可能不是您想要的
代码看起来像
@app.route('/search/custom/<category>/<date>/<page>', methods=['GET', 'POST'])
@app.route('/search/custom/', methods=['GET', 'POST'])
def search_custom(category=None, date=None, page=None):
# do stuff
谢谢你,真管用!是的,我只想要一个url。