简体   繁体   English

烧瓶URL功能无法正常工作

[英]flask url function not working properly

I have a an html form that is sending data with POST then I redirect to the same function using GET with the form data as arguments for the function 我有一个使用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'))

function is receiving the arguments correctly yet its only redirecting to "home": 函数正确接收了参数,但仅将其重定向到“ 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 -

category, date, and page are all None when processing a get request. 处理获取请求时,类别,日期和页面均为None。

Inside of the Get handler, you'll need to actually pull the parameters from the query string. 在Get处理程序内部,您实际上需要从查询字符串中提取参数。

Something like: 就像是:

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

should do the trick. 应该可以。

Put the before the check for the parameters and it should work. 在检查参数之前放置,它应该可以工作。

I didn't have a chance to test this, so let me know if it doesn't work and I'll actually dig into it a bit more. 我没有机会进行测试,所以让我知道它是否不起作用,我将进一步进行深入研究。

The way you have it now, you would probably want to have a formatted url for. 现在的方式,您可能想要一个格式化的URL。 Something like /search/custom/<category>/<date>/<page> . 类似于/search/custom/<category>/<date>/<page> This would require changing the format of the incoming url too though, and that probably isn't what you want. 但是,这也需要更改传入URL的格式,而这可能并不是您想要的。

The code would look something like 该代码看起来像

@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