【问题标题】:How to read input from html form to python script? [duplicate]如何从 html 表单读取输入到 python 脚本? [复制]
【发布时间】:2020-04-28 10:52:44
【问题描述】:

我正在尝试使用 CGI 概念和烧瓶概念将输入从 HTML 表单发送到 python 脚本。但无法读取输入。 设想: 1. 我们需要创建一个 HTML Input 表单。 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>

【问题讨论】:

  • 请给你看html
  • 我也添加了HTML代码。请检查

标签: html python-3.x flask cgi


【解决方案1】:
  1. 您可以删除第二个函数并将 GET 和 POST 请求集成到一种方法中。

2 将表单操作更改为此&lt;form action='/' method='POST'&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多