【问题标题】:How to read in user input on a webpage in Flask [duplicate]如何在Flask的网页上读取用户输入[重复]
【发布时间】:2019-09-10 03:13:52
【问题描述】:

我正在尝试创建一个在根登录页面上有一个 HTML 表单的基本 Web 应用程序,然后在提交后,使用所需的输入运行 postgresql 查询,并将用户重定向到具有生成的 matplotlib 图表的页面他们的意见。在我的主要功能中,我有以下内容:

@app.route('/', methods=['POST'])
def main():
    return render_template("main.html")

假设我的主 html 文件由烧瓶应用程序呈现。我在下面还有另一条路线:

@app.route('/query', methods=['POST'])
def queryPage():
    # code to execute query with information 
    # passed from the main.html template
    # then generate chart via matplotlib via returned information
    return render_template("query.html")

我对如何从 main.html 中的表单获取输入以将信息发送回应用程序以在 /query 端点呈现感到困惑。如果有人能详细说明这一点,我将不胜感激。前端不是我的强项。谢谢!

【问题讨论】:

    标签: python python-3.x matplotlib flask


    【解决方案1】:

    你需要一个 main.html 上的表单...可能像这样(注意表单操作):

    <form action = /query method="POST">
            <label for="username">USERNAME:</label>
            <input type="text" name="username" id="username" size="15">
            <input type="submit" name="submit" id="submit"/>
    </form>
    

    当该表单被发送时(可以说是在用户单击按钮之后),您的烧瓶代码中与操作匹配的路由(在本例中为 /query)将被调用并执行。此外,任何表单元素中的 name= 变量都将在后端的请求中可用(我以变量 username 为例)。你可以像这样得到它们:request.form['username']。其他表单变量(如复选框)会略有不同。

    无论如何,在您的情况下,您需要在 main.html 中某处的 html 中执行 /query 操作。它可以通过按钮或定时 javascript 等调用...

    当在 main.html 上调用此 /query 操作时,您需要

    return render_template('query.html, username=username)
    

    然后用户名变量将在query.html 页面上可用。

    请记住,我只传递了一个变量。您可以传递多个变量、列表、字典等...

    还请记住,您返回到 query.html 的任何变量都可以使用 Jinja 模板变得非常动态。您可以遍历列表并打印不同的 html 标签等,并在您的 html 中使用逻辑......可能取决于返回到页面的值。

    【讨论】:

      【解决方案2】:

      如果我正确理解了您的问题,那么您将难以将表单信息从主函数传递到单独的 queryPage 函数以进行呈现。这可以通过提供您希望作为关键字参数传递给 url_for 函数的值来轻松实现。然后可以从 queryPage 函数中的 request.args 检索这些。鉴于您从该函数返回 query.html 而不是图像,我假设您打算在 query.html 的 img 标记内显示图表。在这种情况下,您将需要另一个视图函数来生成并返回图像本身。您可能还需要为此端点禁用浏览器缓存,以防止浏览器将您的动态图像视为静态图像https://stackoverflow.com/a/2068407/10548137

      @app.route('/', methods=['GET', 'POST'])
      def main():
          form = MyForm(request.form)
          if request.method == "POST" and form.validate():
              return redirect(url_for("queryPage", **form.data))
          return render_template("main.html", form=form)
      
      @app.route('/query', methods=['GET'])
      def queryPage():
          arguments = request.args.to_dict()
          image_url = url_for("make_chart", **arguments)
          return render_template("query.html", image_url=image_url)
      
      @app.route('/make_chart', methods=['GET'])
      def make_chart():
          arguments = request.args.to_dict()
          # perform postgres query here using arguments
          # generate matplotlib chart here using query results
          # ? save chart in BytesIO buffer in png format        
          response = send_file(file_pointer, mimetype="image/png")
          # just return response here if don't need to alter headers
          response = make_response(response)
          response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
          response.headers["Pragma"] = "no-cache"
          response.headers["Expires"] = "0"
          return response
      

      【讨论】:

        猜你喜欢
        • 2014-09-02
        • 1970-01-01
        • 2021-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-12
        • 1970-01-01
        • 2019-03-05
        相关资源
        最近更新 更多