[英]Flask - How to pass user input from HTML into python script
I am trying to get user input for a 'username' and 'password' from a form in HTML so I can input them into my python script and have the program run.我正在尝试从 HTML 的表单中获取用户输入的“用户名”和“密码”,因此我可以将它们输入到我的 python 脚本中并运行程序。 Where do I need to put the request.form to receive both variables?
我需要将 request.form 放在哪里来接收这两个变量?
app.py应用程序.py
from flask import Flask, render_template, url_for, request
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def home():
rh = Robinhood()
rh.login(username="EMAIL", password="PASSWORD")
projectpath = request.form['projectFilepath']
return render_template('index.html')
index.html索引.html
<form action="{{ url_for('home') }}" method="post">
<input type="text" name="projectFilepath">
<input type="submit">
</form>
Very new to flask and python, thanks for the help! flask 和 python 非常新,感谢您的帮助!
In your html, follow the input type with a placeholder and name;在您的 html 中,在输入类型后面加上占位符和名称; along with an error message that's optional but advised (put outside of the form div);
以及可选但建议的错误消息(放在表单 div 之外); give each method of 'username' and 'password' their request.form values respectively:
分别为“用户名”和“密码”的每个方法提供其 request.form 值:
<div class="form">
<form action="" method="post">
<input type="text" placeholder="Username" name="username" value="{{
request.form.username }}">
<input type="password" placeholder="Password" name="password" value="{{
request.form.password }}">
<input class="btn btn-default" type="submit" value="Login">
</form>
{% if error %}
<p class="error">Error: {{ error }}
{% endif %}
</div>
In your python file, put your request.form following an if statement under /login with the methods POST and GET;在您的 python 文件中,使用 POST 和 GET 方法将您的 request.form 放在 /login 下的 if 语句之后; include your newly made error message:
包括您新制作的错误消息:
@app.route('/login', methods=['POST', 'GET'])
def home():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Error Message Text'
else:
return redirect(url_for('home'))
return render_template('index.html', error=error)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.