将数据从HTML表单发送到Flask中的Python脚本 [英] Sending data from HTML form to a Python script in Flask
本文介绍了将数据从HTML表单发送到Flask中的Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def cmd_wui(argv,path_to_tx):
运行Web UI。$ $ $ b从瓶子导入烧瓶,闪光灯,jsonify,render_template,请求
导入webbrowser
app =烧瓶(__ name__)
@ app.route('/ tx / index /')
def index():
加载启动页面,在其中选择项目文件夹
或加载历史记录项目从本地数据库。$
从txclib导入get_version
txc_version = get_version()
prj = project.project(path_to_tx)
#让我们创建一个资源列表从我们的配置文件
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,用户名=用户名)
另外,我在init.html中有一个HTML表单:
< form>
< input type =textid =projectFilepathsize =40placeholder =找到您的项目文件>
< input type =buttonid =spotButtonvalue =Spot>
< / form>
当用户点击变量spotButton时,如何从projectFilepath传递用户输入在我的python脚本?
我是Python和Flask的新手,所以如果我犯了什么错误,请原谅我。
表单标记需要设置两个属性:
-
action
:表单数据在提交时发送到的URL。使用url_for
生成。如果显示表单和处理数据的URL句柄相同,则可以省略。查询字符串(GET)或表单数据(POST)。
添加一个视图来处理表单数据:
@ app.route('/ handle_data',methods = ['POST'])
def handle_data():
projectpath = request.form ['projectFilepath']
#你的代码
#返回一个响应
将表单的动作
设置为该视图的网址:
< form action ={{url_for('handle_data')}}method =post>
< input type =textname =projectFilepath>
< input type =submit>
< / form>
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)
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>
How can I pass the user input from "projectFilepath" when a user clicks "spotButton" on a variable in my python script?
I'm new in Python and Flask, so forgive me if I make any mistakes.
解决方案
The form
tag needs two attributes set:
action
: The URL that the form data is sent to on submit. Generate it withurl_for
. It can be omitted if the same URL handles showing the form and processing the data.method
: Whether to submit the data as a query string (GET) or form data (POST).
Add a view to handle the form data:
@app.route('/handle_data', methods=['POST'])
def handle_data():
projectpath = request.form['projectFilepath']
# your code
# return a response
Set the form's action
to that view's URL:
<form action="{{ url_for('handle_data') }}" method="post">
<input type="text" name="projectFilepath">
<input type="submit">
</form>
这篇关于将数据从HTML表单发送到Flask中的Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文