繁体   English   中英

烧瓶URL功能无法正常工作

[英]flask url function not working properly

我有一个使用POST发送数据的html表单,然后使用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')
        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'))

函数正确接收了参数,但仅将其重定向到“ home”:

127.0.0.1 - - [10/Apr/2018 01:55:36] "GET /search/custom/?category=Vetements&date=Dernier+mois&page=1 HTTP/1.1" 302 -

处理获取请求时,类别,日期和页面均为None。

在Get处理程序内部,您实际上需要从查询字符串中提取参数。

就像是:

category = request.args.get('category')
date = request.args.get('date')
page = request.args.get('page')

应该可以。

在检查参数之前放置,它应该可以工作。

我没有机会进行测试,所以让我知道它是否不起作用,我将进一步进行深入研究。

现在的方式,您可能想要一个格式化的URL。 类似于/search/custom/<category>/<date>/<page> 但是,这也需要更改传入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

暂无
暂无

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

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