-1

I am trying to send input from HTML form to python script using the CGI concept and flask concept. But unable to read the input. Scenario: 1. We need to create an HTML Input form. 2. Read input from the form and send to python script. I am attaching my code as well. If anybody finds the issue pls let me know. Code Used:

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)

I have used this Html Code:

<!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>
user12644602
  • 11
  • 2
  • 7

1 Answers1

0
  1. You can remove the second function and integrate the GET and POST requests in one method.

2 Change the form action to this <form action='/' method='POST'>

DevHyperCoder
  • 895
  • 1
  • 9
  • 22