我想使用python,html和javascript构建桌面应用程序。到目前为止,我已经按照烧瓶的方式进行了调查,并有一个hello world工作示例。我现在应该怎么做才能让它发挥作用? html文件如何与他们下面的python脚本“对话”?
到目前为止,这是我的代码:
from flask import Flask, url_for, render_template, redirect
app = Flask(__name__)
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
@app.route('/')
def index():
return redirect(url_for('init'))
@app.route('/init/')
def init():
css = url_for('static', filename='zaab.css')
return render_template('init.html', csse=css)
if __name__ == '__main__':
app.run()
答案 0 :(得分:3)
您可以像在Jinja模板中一样使用HTML表单 - 然后在您的处理程序中使用以下内容:
from flask import Flask, url_for, render_template, redirect
from flask import request # <-- add this
# ... snip setup code ...
# We need to specify the methods that we accept
@app.route("/test-post", methods=["GET","POST"])
def test_post():
# method tells us if the user submitted the form
if request.method == "POST":
name = request.form.name
email = request.form.email
return render_template("form_page.html", name=name, email=email)
如果您想使用GET
POST
的{{1}}来提交表单,只需检查request.args
而不是request.form
(有关详细信息,请参阅flask.Request
's documentation )。如果您要对表单做很多事情,我建议您查看优秀的WTForms项目和Flask-WTForms extension。