Flask Redirect Again After Redirecting to a Page
The redirect() function allows the states to redirect a user to the URL of our choice. In the Flask application that nosotros are edifice then far, nosotros have a /shortenurl route that checks to see what method type is in use. If information technology is a GET request, we are simply returning some text to the user. Instead of that, nosotros can redirect them to the homepage so they tin enter a URL and shortcode. The employ of redirect is highlighted in the code here.
flask-tutorial\app.py
from flask import Flask, render_template, request, redirect app = Flask(__name__) @app.road('/') def dwelling house(): render render_template('home.html') @app.route('/shortenurl', methods=['Become', 'Postal service']) def shortenurl(): if request.method == 'Post': return render_template('shortenurl.html', shortcode=request.form['shortcode']) elif asking.method == 'Become': return redirect('/') else: return 'Not a valid request method for this route' Now try going to 127.0.0.ane:5000/shortenurl directly in the web browser. You lot will find that yous are instantly redirected to the habitation (/) route and the form we created before is displayed.
url_for()
Another method yous can employ when performing redirects in Flask is the url_for() function. The manner that url_for() works is instead of redirecting based on the cord representation of a route, y'all provide the function name of the route you want to redirect to. So if we update the code to the snippet here, we get the aforementioned result merely using url_for() instead of redirect(). Also annotation that both redirect, and url_for are imported on the first line of app.py.
from flask import Flask, render_template, request, redirect, url_for app = Flask(__name__) @app.route('/') def dwelling(): return render_template('home.html') @app.route('/shortenurl', methods=['Become', 'POST']) def shortenurl(): if request.method == 'POST': render render_template('shortenurl.html', shortcode=request.form['shortcode']) elif request.method == 'GET': render redirect(url_for('domicile')) else: return 'Non a valid request method for this route' Shortcode To URL Redirects
This app takes 2 inputs, a URL you would like to visit and the short code that represents that URL. And then for instance, if you type yhoo, you would like to be sent to https://yahoo.com. To become started with this, permit'south set up a JSON file to store the curt code / URL combinations that the app receives as input. The post-obit highlighted code stores each shortcode / URL combination and makes sure to check at that place are no duplicates to ensure that each shortcode is unique for only one given URL.
from flask import Flask, render_template, request, redirect, url_for import json import os.path app = Flask(__name__) @app.route('/') def dwelling house(): return render_template('home.html') @app.route('/shortenurl', methods=['GET', 'Mail service']) def shortenurl(): if asking.method == 'Mail': urls = {} if bone.path.exists('urls.json'): with open('urls.json') as url_storage: urls = json.load(url_storage) if request.class['shortcode'] in urls.keys(): render redirect(url_for('home')) urls[request.form['shortcode']] = request.course['url'] with open('urls.json', 'west') as url_storage: json.dump(urls, url_storage) render render_template('shortenurl.html', shortcode=request.form['shortcode']) elif asking.method == 'Become': render redirect(url_for('home')) else: return 'Not a valid request method for this road' Now when we add another shortcode / URL combination, information technology shows up in the JSON storage that was created.
In the Flask project root, in that location is now a urls.json file and when nosotros open it, the information we have submitted so far is there.
Performing Redirect Of Shortcode To URL
At this betoken, we can add the lawmaking that volition take a user from the shortcode to an actual website. So for example, using the data we have already stored in the JSON file, we would like to be able to visit http://127.0.0.i:5000/yhoo and the event will be loading https://yahoo.com. Too, http://127.0.0.1:5000/twtr should load https://twitter.com. The mode to do this is with a variable road that uses the shortcode itself equally the route. In the following shortcode_redirect() function, we capture the shortcode that is typed into the browser. In other words, visiting http://127.0.0.one:5000/yhoo means 'yhoo' is the shortcode. Then, we check for the existence of that shortcode in the JSON storage that has been set up. If information technology exists, then we redirect the user to the full URL that is associated with that given shortcode.
from flask import Flask, render_template, request, redirect, url_for import json import os.path app = Flask(__name__) @app.route('/') def abode(): return render_template('dwelling house.html') @app.route('/shortenurl', methods=['Become', 'POST']) def shortenurl(): if request.method == 'POST': urls = {} if bone.path.exists('urls.json'): with open('urls.json') equally url_storage: urls = json.load(url_storage) if request.form['shortcode'] in urls.keys(): render redirect(url_for('home')) urls[request.course['shortcode']] = asking.form['url'] with open('urls.json', 'west') as url_storage: json.dump(urls, url_storage) return render_template('shortenurl.html', shortcode=asking.form['shortcode']) elif request.method == 'Get': return redirect(url_for('domicile')) else: render 'Not a valid request method for this route' @app.route('/<cord:shortcode>') def shortcode_redirect(shortcode): if os.path.exists('urls.json'): with open('urls.json') as url_storage: urls = json.load(url_storage) if shortcode in urls.keys(): return redirect(urls[shortcode]) Sure enough, this is working now.
Learn More About How To Redirect In Flask
- Redirecting To Url In Flask (stackoverflow.com)
- Flask Quickstart (flask.palletsprojects.com)
- Flask Redirect (tutorialspoint.com)
- Flask Redirect Examples (fullstackpython.com)
- How To Redirect To A Url Using Flask In Python (kite.com)
- Python Modules Flask Redirect Url (askpython.com)
- Flask Redirect And Errors (javatpoint.com)
- Flask Redirect (python-commandments.org)
- Flask Topic Redirect (sodocumentation.internet)
- Flask Generated Flask.redirect.html (tedboy.github.io)
- How To Handle Python Flask Redirect (stackoverflow.com)
- Python Flask Redirect To Https From Http (stackoverflow.com)
- Flask Redirection Non Working (stackoverflow.com)
Source: https://vegibit.com/how-to-redirect-to-url-in-python-flask/
0 Response to "Flask Redirect Again After Redirecting to a Page"
แสดงความคิดเห็น