Redirecting to another page with button click in Python-flask

 Redirecting to another page with  click in Python-flask

Flask is a backend server that is built entirely using Python. It is a  framework that consists of Python packages and modules. It is lightweight which makes developing backend applications quicker with its features.

Suppose we have to attach any html page Link in any webpage in python 
<a class="dropdown-item" href="{{ url_for('terms') }}"><span
class="item-text">Terms Conditions</span></a>


<div class="dropdown-items-divide-hr"></div>

<a class="dropdown-item" href="{{ url_for('privacy') }}"><span
class="item-text">Privacy Policy</span></a>

Redirect to a URL in Flask

A redirect is used in the Flask class to send the user to a particular URL with the status code. conversely, this status code additionally identifies the issue. When we access a website, our browser sends a request to the server, and the server replies with what is known as the HTTP status code, which is a three-digit number.

Syntax of Redirect in Flask

Syntax: flask.redirect(location,code=302)

Parameters:

  • location(str): the location which URL directs to.
  • code(int): The status code for Redirect.
  • Code: The default code is 302 which means that the move is only temporary.

Return: The response object and redirects the user to another target location with the specified code.

Redirecting to another page with  click in Python-flask
THEN GO TO UR MAIN.PY PAGE :
@app.route('/terms')===> it is the name of file which we have mentioned inside href of above
def terms():====> keep it same as name above
return render_template('terms-conditions.html')====then this page will open when click avove
link:::Terms Conditions - Evolo - StartUp HTML Landing Page Template



@app.route('/privacy') ===> it is the name of file which we have mentioned inside href of above
def privacy()::====> keep it same as name above
return render_template('privacy-policy.html')====then this page will open when click
 avove:

url_for() Function in Flask


Another method you can use when performing redirects in Flask is the url_for() function URL building. This function accepts the name of the function as the first argument, and the function named user(name) accepts the value through the input URL. It checks whether the received argument matches the ‘admin’ argument or not. If it matches, then the function hello_admin() is called else the hello_guest() is called.

A redirect is used in the Flask class to send the user to a particular URL with the status code. conversely, this status code additionally identifies the issue. When we access a website, our browser sends a request to the server, and the server replies with what is known as the HTTP status code, which is a three-digit number.

Comments

Popular posts from this blog

Python to automate What's App messages

Install WAMP server to run python