Flask Redirect and Errors



 Redirect

Flask Redirect

  • redirect() function - Redirects the user to some specified URL with the specified status code.

Syntax

 Re-direct1

Redirect& Error

Flask.redirect(<location>,<status-code>, <response> )  
  • Location - It is the URL where the response will be redirected.
  • Status-code - It is the status code that is sent to the browser's header along with the response from the server.
  • Response - It is Instance of the response which is used in the project for future requirements.

Sample Code

login.py

from flask import *  
app = Flask(__name__)  
 @app.route('/')  
def home ():  
    return render_template("home.html")  
@app.route('/login')  
def login():  
   return render_template("login.html");  
 @app.route('/validate', methods = ["POST"])  
def validate():  
    if request.method == 'POST' and request.form['pass'] == 'jtp':  
        return redirect(url_for("success"))  
    return redirect(url_for("login"))  
@app.route('/success')  
def success():  
  return "logged in successfully"  
  if __name__ == '__main__':  
    app.run(debug = True)  

home.html

<html>  
<head>  
<title>home</title>  
</head>  
<body>  
<h3>Welcome to the website</h3>  
<a href = "/login">login</a><br>  
</html>  

login.html

<html>  
<head>  
    <title>login</title>  
</head>  
<body>  
    <form method = "post" action = "http://localhost:5000/validate">  
        <table>  
            <tr><td>Email</td><td><input type = 'email' name = 'email'></td></tr>  
            <tr><td>Password</td><td><input type = 'password' name = 'pass'></td>
</tr>  
            <tr><td><input type = "submit" value = "Submit"></td></tr>  
        </table>  
    </form>  
</body>  
</html>  

Output

  • In the example, the URL '/' contains a link to the login page as shown in the following screenshot.
 Home

Homepage

  • If we click the login then, the application shows the login page. The user enter the data email id and password and the submit button redirects the user to URL /validate.
 Flask Redirated Login

Flask Redirated Login

  • The user is redirected to the URL /success only if the password entered by the user to 'jtp'
 Flask Dir Sucess

Flask Dir Sucess

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.

abort() function

  • abort() function - It 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.

Syntax

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

Now modify the script login.py from the above code and use the abort() function with the error code 401 (for unauthorized access) in the case of any random password entered by the user.

Sample Code

from flask import *  
app = Flask(__name__)  
 
@app.route('/')  
def home ():  
    return render_template("home.html")  
 
@app.route('/login')  
def login():  
    return render_template("login.html");  
 
@app.route('/validate', methods = ["POST"])  
def validate():  
    if request.method == 'POST' and request.form['pass'] == 'jtp':  
        return redirect(url_for("success"))  
    else:  
        abort(401)  
 
@app.route('/success')  
def success():  
    return "logged in successfully"  
  
if __name__ == '__main__':  
    app.run(debug = True)  

Output

 Flask abort() function

Flask abort() function

If you want to learn about Python Course , you can refer the following links Python Training in Chennai , Machine Learning Training in Chennai , Data Science Training in Chennai , Artificial Intelligence Training in Chennai



Related Searches to Flask Redirect and Errors