article

Wednesday, January 22, 2020

Flask Redirect and Errors

Flask Redirect and Errors

Flask class provides the redirect() function which redirects the user to some specified URL with the specified status code.

An HTTP status code is a response from the server to the request of the browser. When we visit a website, a request is sent to the server, and the server then responds to the browser's request with a three-digit code: the HTTP status code. This status code also represents the error.


The syntax to use the redirect() function is given below.

app.py
from flask import Flask, render_template, request, redirect, url_for
# Initialize the Flask application
app = Flask(__name__)


@app.route('/')
def index():
   return render_template('log_in.html')
   
@app.route('/login',methods = ['POST', 'GET']) 
def login(): 
   if request.method == 'POST' and request.form['txtemail'] == 'tutorial101@blogspot.com' and request.form['txtpass'] == 'ednalan' :
      return redirect(url_for('success'))
   else:
      return redirect(url_for('errorlogin'))

@app.route('/success')
def success():
   return '<h1>logged in successfully</h1>'
 
@app.route('/errorlogin')
def errorlogin():
   return '<h1>Bad Credentials. Please login again <a href = "/">login</a></h1>'
   
if __name__ == '__main__':
   app.run(debug = True)
//log_in.html
<html>
   <body>
 <form method = "post" action = "/login">  
  <p><h3>Login Account</h3></p>
        <table>  
            <tr><td>Email</td><td><input type = 'email' name = 'txtemail'></td></tr>  
            <tr><td>Password</td><td><input type = 'password' name = 'txtpass'></td></tr>  
            <tr><td><input type = "submit" value = "Submit"></td></tr>  
        </table>  
    </form> 
   </body>
</html>

Errors

Standard HTTP Codes
The following HTTP codes are standardized.

HTTP_300_MULTIPLE_CHOICES
HTTP_301_MOVED_PERMANENTLY
HTTP_302_FOUND
HTTP_303_SEE_OTHER
HTTP_304_NOT_MODIFIED
HTTP_305_USE_PROXY
HTTP_306_RESERVED
HTTP_307_TEMPORARY_REDIRECT
The default status code is HTTP_302_FOUND.

The abort() function
The abort() function is used to handle the cases where the errors are involved in the requests from the client side, such as bad requests, unauthorized access and many more. However, the error code is to be mentioned due to which the error occurred.

The syntax to use the abort() function is given below.

Flask.abort(code) 

The Following error codes depending upon the specified errors.

400: for bad requests
401: for unauthorized access
403: for forbidden
404: for not found
406: for not acceptable
415: for unsupported media types
429: for too many requests
from flask import Flask, render_template, request, redirect, url_for, abort
# Initialize the Flask application
app = Flask(__name__)


@app.route('/')
def index():
   return render_template('log_in.html')
   
@app.route('/login',methods = ['POST', 'GET']) 
def login(): 
   if request.method == 'POST' and request.form['txtemail'] == 'tutorial101@blogspot.com' and request.form['txtpass'] == 'ednalan' :
      return redirect(url_for('success'))
   else:
      abort(401)

@app.route('/success')
def success():
   return '

logged in successfully

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

Related Post