将数据从 HTML 表单发送到 Flask 中的 Python 脚本 [英] Sending data from HTML form to a Python script in Flask

查看:49
本文介绍了将数据从 HTML 表单发送到 Flask 中的 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Python 脚本中有以下代码:

I have the code below in my Python script:

def cmd_wui(argv, path_to_tx):
    """Run a web UI."""
    from flask import Flask, flash, jsonify, render_template, request
    import webbrowser
    app = Flask(__name__)


    @app.route('/tx/index/')
    def index():
        """Load start page where you select your project folder
        or load history projects from local DB."""
        from txclib import get_version
        txc_version = get_version()
        prj = project.Project(path_to_tx)

        # Let's create a resource list from our config file
        res_list = []
        prev_proj = ''
        for idx, res in enumerate(prj.get_resource_list()):
                hostname = prj.get_resource_host(res)
        username, password = prj.getset_host_credentials(hostname)
        return render_template('init.html', txc_version=txc_version, username=username)

另外,我在 init.html 中有一个 HTML 表单:

Also, I have an HTML form in init.html:

<form>
<input type="text" id="projectFilepath" size="40" placeholder="Spot your project files">
<input type="button" id="spotButton" value="Spot">
</form>

当用户在我的 Python 脚本中的变量上单击spotButton"时,如何传递来自projectFilepath"的用户输入?

How can I pass the user input from "projectFilepath" when a user clicks "spotButton" on a variable in my python script?

我是 Python 和 Flask 的新手,如有错误请见谅.

I'm new in Python and Flask, so forgive me if I make any mistakes.

推荐答案

form 标签需要设置一些属性:

The form tag needs some attributes set:

  1. action:表单数据在提交时发送到的 URL.使用 url_for 生成它.如果相同的 URL 处理显示表单和处理数据,则可以省略它.
  2. method="post":使用 POST 方法将数据作为表单数据提交.如果未给出,或明确设置为 get,则数据将在查询字符串 (request.args) 中使用 GET 方法提交.
  3. enctype="multipart/form-data":当表单包含文件输入时,必须设置此编码,否则文件将无法上传,Flask 将看不到它们.
  1. action: The URL that the form data is sent to on submit. Generate it with url_for. It can be omitted if the same URL handles showing the form and processing the data.
  2. method="post": Submits the data as form data with the POST method. If not given, or explicitly set to get, the data is submitted in the query string (request.args) with the GET method instead.
  3. enctype="multipart/form-data": When the form contains file inputs, it must have this encoding set, otherwise the files will not be uploaded and Flask won't see them.

input 标签需要一个 name 参数.

The input tag needs a name parameter.

添加一个视图来处理提交的数据,它在与输入的name 相同的键下的request.form 中.任何文件输入都将在 request.files 中.

Add a view to handle the submitted data, which is in request.form under the same key as the input's name. Any file inputs will be in request.files.

@app.route('/handle_data', methods=['POST'])
def handle_data():
    projectpath = request.form['projectFilepath']
    # your code
    # return a response

使用 url_for 将表单的 action 设置为该视图的 URL:

Set the form's action to that view's URL using url_for:

<form action="{{ url_for('handle_data') }}" method="post">
    <input type="text" name="projectFilepath">
    <input type="submit">
</form>

这篇关于将数据从 HTML 表单发送到 Flask 中的 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆