【问题标题】:Why isn't my GET request parameters showing up in my url? [duplicate]为什么我的 GET 请求参数没有显示在我的 url 中? [复制]
【发布时间】:2019-06-13 01:52:22
【问题描述】:

我的模板文件夹中有 2 个 html 文件:home.html 和 search.html。我有一个烧瓶后端,基本上每次我运行我的应用程序时,它都会首先加载我的 home.html 文件。我的主页上有一个搜索栏,这个想法是每当有人在搜索栏中输入内容并按 Enter 键时,我的 search.html 页面就会加载并发出 GET 请求。

我主页的搜索栏是表格,下面是代码。

<form class="form-inline" role="search" method="get">
    <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name="q">
    <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  </form>

我希望我的网址更改为典型的 GET 请求格式:“http://.../search?q?=whateverTextWasEnteredIntoTheForm

但是,每当我在主页上输入表单并点击搜索按钮时,我的应用程序都会按原样导航到我的 search.html 页面,但我的查询似乎完全丢失了。我留下的网址只是“http://.../search?”为什么?

到目前为止,我还包括了两种烧瓶方法。

这是一个问题,因为我最终需要能够访问我的搜索参数,以便显示我的搜索结果。

from flask import Flask, render_template, request


app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/search', methods=['GET'])
def search():
    if request.method == 'GET':
        #print("hi")
        PARAMS= request.args.getlist('q[]')
        print(PARAMS)


    return render_template('search.html')

【问题讨论】:

    标签: html twitter-bootstrap http flask


    【解决方案1】:

    您的表单需要action 属性才能转到正确的路线:

    <form action="{{url_for('search')}}" ...>
    

    (将输出action="/search"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 2019-11-05
      • 2019-01-05
      • 2019-10-08
      • 1970-01-01
      • 2018-08-03
      相关资源
      最近更新 更多