Flask - 如何将用户输入从 HTML 传递到 python 脚本 [英] Flask - How to pass user input from HTML into python script

查看:57
本文介绍了Flask - 如何将用户输入从 HTML 传递到 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 HTML 表单中获取用户输入的用户名"和密码",以便我可以将它们输入到我的 Python 脚本中并运行程序.我需要将 request.form 放在哪里才能接收这两个变量?

app.py

from flask import Flask, render_template, url_for, requestapp = Flask(__name__)@app.route('/', methods=['POST', 'GET'])定义家():rh = Robinhood()rh.login(username="EMAIL", password="PASSWORD")projectpath = request.form['projectFilepath']返回 render_template('index.html')

index.html

<input type="text" name="projectFilepath"><输入类型=提交"></表单>

对flask 和python 非常陌生,感谢您的帮助!

解决方案

在你的 html 中,在输入类型后面加上一个占位符和名称;以及可选但建议的错误消息(放在表单 div 之外);分别为用户名"和密码"的每个方法提供它们的 request.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="登录"></表单>{% 如果错误 %}<p class="error">错误:{{ error }}{% 万一 %}

在您的 python 文件中,将 request.form 放在/login 下的 if 语句之后,并使用 POST 和 GET 方法;包括您新制作的错误消息:

@app.route('/login', methods=['POST', 'GET'])定义家():错误 = 无如果 request.method == 'POST':如果 request.form['username'] != 'admin' 或 request.form['password'] != 'admin':error = '错误消息文本'别的:返回重定向(url_for('home'))return render_template('index.html', error=error)

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. Where do I need to put the request.form to receive both variables?

app.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

<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!

解决方案

In your html, follow the input type with a placeholder and name; along with an error message that's optional but advised (put outside of the form div); give each method of 'username' and 'password' their request.form values respectively:

<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; 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)

这篇关于Flask - 如何将用户输入从 HTML 传递到 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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