4

I am planning to get input from an HTML Form when submitted, the input will be send over to Python. Here is the HTML File

<form method="GET">

  <div class="form-inline">
    <div class="form-group">
      <input type="email" class="line-input" name="userEmail" placeholder="Email">
    </div>
    <div class="form-group">
      <input type="password" class="line-input" name="userPassword" placeholder="Password">
    </div>
  </div>

  <div class="form-inline">
    <div class="form-group">
      <input type="email" class="line-input" name="recipientMail" placeholder="Recipient">
    </div>
    <div class="form-group">
      <input type="email" class="line-input" name="CCEmail" placeholder="CC">
    </div>
  </div>

  <div class=" form-group">
    <button type="submit" class="btn btn-light text-primary btn-block" style="margin : 20px 20px -10px 0px">Send Message</button>
  </div>

  </form>

Now I don't know the best way to do this, I tried this in Python but doesn't seem to work

@app.route('/', methods=['POST'])
def form_post():
    userEmail = request.form['userEmail']
    userPassword = request.form['userPassword']
    return userEmail, userPassword

Can anyone help me out here?

1 Answer 1

4

Change your form method from GET to POST, as your route only specifies "POST", and will not accept any other requests of a different type:

<form method="POST">

Edit: if you wish to specify both methods, ensure that your route checks for the correct type of request currently being sent when the route is triggered:

@app.route('/', methods=['POST','GET'])
def form_post():
  if flask.request.method == 'POST'
     userEmail = request.form['userEmail']
     userPassword = request.form['userPassword']
     return userEmail, userPassword
  return flask.render_template('something.html')

Note, however, that you are creating your form on the home route ('/'). It may be best to return a link to the page that has the form code:

@app.route('/')
def home():
  return 'Welcome! <a href="/login">login here</a>'

@app.route('/login', methods=['GET', 'POST']):
  if flask.request.method == 'POST'
    userEmail = request.form['userEmail']
    userPassword = request.form['userPassword']
    return flask.redirect('/')
  return flask.render_template('form_filename.html')
Sign up to request clarification or add additional context in comments.

8 Comments

please can you add more to your answer so I know what to do next time? Maybe a link of tutorial or reference?
@AbhishtaGatya That is really all there is to it. Your code is perfectly fine otherwise.
What if I specify both? sorry for being needy
@AbhishtaGatya Please see my recent edit. I added a cleaner method of retrieving the form data.
@AbhishtaGatya you need to specify both so that the code will know when to access the form data structure values and when to merely serve the file. If you attempt to access the form results when the user has merely sent a GET request (simple page view) an error will occur.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.