我的Python脚本中有以下代码:
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表单:
<form> <input type="text" id="projectFilepath" size="40" placeholder="Spot your project files"> <input type="button" id="spotButton" value="Spot"> </form>
当用户在python脚本中的变量上单击“ spotButton”时,如何传递来自“ projectFilepath”的用户输入?
我是Python和Flask的新手,所以如果我犯任何错误,请原谅我。
该form标记需要两个属性进行设置:
action
url_for
method
该input标签需要一个name参数。
input
name
添加一个视图来处理提交的数据,该视图request.form位于与输入相同的键下name。
request.form
@app.route('/handle_data', methods=['POST']) def handle_data(): projectpath = request.form['projectFilepath'] # your code # return a response
使用以下命令将表单设置action为该视图的URL url_for:
<form action="{{ url_for('handle_data') }}" method="post"> <input type="text" name="projectFilepath"> <input type="submit"> </form>