0

I am building a web application and trying to link two html by using the redirect() function. However, when I click the 'submit' button, the login.html is not redirecting to the success.html(i.e., the browser still in login.html)

Below is my python code:

from flask import Flask, redirect, url_for, render_template, request, abort
app = Flask(__name__)

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

@app.route('/login',methods = ['POST', 'GET'])
def login():
   if request.method == 'POST':
      if request.form['username'] == 'admin':
         return redirect(url_for('success'))
      else:
         abort(401)
   else:
      return redirect(url_for('index'))

@app.route('/success', methods = ['POST', 'GET'])
def success():
   return 'logged in successfully'

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

And also the html code:

<html>
   <body>
      <form action = "login" method = "post">
         <p>Enter Name:</p>
         <p><input type = "text" name = "username" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
   </body>
</html>
Dee
  • 31
  • 5

1 Answers1

0

Your field name does not match in html and view function. Change name = "nm" to name = "username":

<html>
   <body>
      <form action = "login" method = "post">
         <p>Enter Name:</p>
         <p><input type = "text" name = "username" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
   </body>
</html>
maanijou
  • 1,067
  • 1
  • 14
  • 23