如何从 html 表单读取输入到 python 脚本?

How to read input from html form to python script?

我正在尝试使用 CGI 概念和 flask 概念将输入从 HTML 表单发送到 python 脚本。但无法读取输入。 设想: 1.我们需要创建一个HTML输入表单。 2. 从表单中读取输入并发送到 python 脚本。 我也附上我的代码。如果有人发现问题,请告诉我。 使用的代码:

import flask
from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html') 

@app.route('/', methods=['POST'])
def getvalue():
    Name = request.form['yourName']
    EnterpriseId = request.form['enterpriseId']
    Servers = request.form['server']
    tes = "Hello"
    print(tes)
    return render_template('pass.html', n=Name, eId=EnterpriseId, sName=Servers)   

if __name__ == '__main__':
    app.run(debug=True)

我用过这个Html代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

input[type=text], select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  resize: vertical;
}

label {
  padding: 12px 12px 12px 0;
  display: inline-block;
}

input[type=submit] {
  background-color: #4CAF50;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  float: right;
}

input[type=submit]:hover {
  background-color: #45a049;
}

.container {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}

.col-25 {
  float: left;
  width: 25%;
  margin-top: 6px;
}

.col-75 {
  float: left;
  width: 75%;
  margin-top: 6px;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .col-25, .col-75, input[type=submit] {
    width: 100%;
    margin-top: 0;
  }
}
</style>
</head>
<body>

<h2>Enter Your Inputs</h2>
<div class="container">
  <form action="app.py" method="post">
    <div class="row">
      <div class="col-25">
        <label for="name">Enter Your Name</label>
      </div>
      <div class="col-75">
        <input type="text" id="name" name="yourName" placeholder="Your name..">
      </div>
    </div>
    <div class="row">
      <div class="col-25">
        <label for="eId">Enter Your Enterprise Id</label>
      </div>
      <div class="col-75">
        <input type="text" id="eId" name="enterpriseId" placeholder="Your enterprise Id..">
      </div>
    </div>

    <div class="row">
      <div class="col-25">
        <label for="server">Enter Server Name</label>
      </div>
      <div class="col-75">
        <input type="text" id="server" name="server" placeholder="write server..">
      </div>
    </div>
    <div class="row">
      <input type="submit" value="Submit">
    </div>
  </form>
</div>

</body>
</html>
  1. 您可以删除第二个函数,并在一个方法中集成 GET 和 POST 请求。

2 将表单操作更改为此 <form action='/' method='POST'>