Here in this Python flask tutorial guide, we are going to learn all about flask URL building dynamically. In the previous flask tutorial, we covered all about flask app routing using various types of examples.
Note:- To understand Flask coding, you should have basic knowledge of Python programming. To learn Python from scratch to advanced, you can follow our Python programming tutorial.
Headings of Contents
Flask URL Building ( Dynamic URL )
Flask URL building is the process of defining the URLs in flask-based applications. Flask provides the best function call url_for()
to define the URLs in the flask-based applications. In this guide, we will define the different URLs dynamically using the url_for()
method.
To build a URL for a specific function, use url_for()
function. It accepts the name of the function as the first argument and any number of the keyword arguments, each corresponding to the variable part of the URL.
Let’s see some benefits of using the url_for()
function to define the URLs in the flask application instead of defining hard-coded URLs.
- Avoid compiling coding for the URLs.
- The generated paths are always absolute, avoiding unexpected behavior of relative paths in browsers.
- You can change the flask application URLs dynamically instead of manually.
let’s understand the flask url_for() method to generate URLs dynamically.
Python Flask URL Building Example:
from flask import Flask, url_for
#Create the object of the Flask class.
app= Flask(__name__)
@app.route("/")
def Home():
return "This is the home page of the website"
@app.route("/about")
def About():
return "This is the about page of the website"
@app.route("/contact")
def Contact():
return "This is the contact page of the website"
@app.route("/user/<username>")
def user(username):
return f"{username} you are successfylly logged in."
with app.test_request_context():
url_for('Home')
url_for('About')
url_for('Contact')
url_for('user', username="John")
if __name__ == '__main__':
app.run(debug=True)
To see the result in the browser, you have to execute the above Python script file and visit http://127.0.0.1:5000, http://127.0.0.1:5000/about, http://127.0.0.1:5000/contact, and http://127.0.0.1:5000/user/John address one by one.
Let’s break down all the above flask script code and see what happens in that code.
Step 1:
Import the Flask class and url_for() function from the flask module.
Step 2:
app= Flask(__name__)
Create the object or instance of the Flask class.
Step 3:
@app.route("/")
def Home():
return "This is the home page of the website"
@app.route("/about")
def About():
return "This is the about page of the website"
@app.route("/contact")
def Contact():
return "This is the contact page of the website"
@app.route("/user/<username>")
def user(username):
return f"{username} you are successfylly logged in."
Define all the above functions with route()
to show the different results based on the URLs.
Step 4:
Redirect to the request URL by using test_request_context()
function.
with app.test_request_context():
url_for('Home')
url_for('About')
url_for('Contact')
url_for('user', username="John")
Step 5:
Run the Flask script file on the local development server by using the run()
method.
if __name__ == '__main__':
app.run(debug=True)
Conclusion
So, In this article, you have learned all about Python Flask URL building using various views functions. In the previous Flask tutorial, we completed flask app routing.
URL is the most important part of any web application and is very helpful for navigating all the website’s content.
If we want to learn the Python Flask web framework from scratch to advanced, Then follow our Flask tutorials. If you like this flask URL-building tutorial, please share and keep visiting for further flask tutorials.
See Also:
- Python Flask Tutorial for Beginners 2021
- First Flask Application
- Setup Flask Environment
- Flask App routing