烧瓶 - 搜索到的字的网址

voy*_*rsm 3 python flask

我想问一下如何让我的网址包含我搜索的内容.像这样的东西:

http://host/filter?test
Run Code Online (Sandbox Code Playgroud)

我的代码:

    @app.route('/filter', methods=['POST', 'GET'])
def filter():
        if request.method == 'POST':
            if str(request.form['search_string']) <> '':
                api.queryMessage(request.form['search_string'])
       return render_template('main.html', search_string=search_string)
Run Code Online (Sandbox Code Playgroud)

我的模板,main.html:

<form name="filters" action="{{ url_for('filter') }}" method=post id="filters">
   <div style="position: absolute; top:100px; left:300px">
       <p>Search string: <input type=text size=80 title="Match all of the words"   name=search_string value="{{search_string}}"></p>
       <input type=submit value=Filter/Search onclick=loopSelected();>
       <input type="hidden" name="chosen" id="chosen" value="{{chosen}}" />
   </div>
</form>
Run Code Online (Sandbox Code Playgroud)

Ale*_*okk 6

您现在正在使用POST请求.使用GET请求,因此浏览器会将您的表单值放入URL.

在HTML表单中set method ="GET":

<form name="filters" action="{{ url_for('filter') }}" method="GET" id="filters">
Run Code Online (Sandbox Code Playgroud)

您将获得以下格式的URL:

http://host/filter?search_string=test&chosen=<id>&<some other trash>
Run Code Online (Sandbox Code Playgroud)

在Flask中使用request.args而不是request.form:

if request.method == 'GET':
    if request.args.get('search_string'):
        ...
Run Code Online (Sandbox Code Playgroud)