Menu Close

Flask URL Building

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.

Flask URL Building ( Dynamic URL )

Flask URL building is the process of defining the URLs in flask-based applications. Flask provides the best function that is 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 argument, 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 compile 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.

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 happing 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-2 result 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 have completed flask app routing.

URL is the most important part of any web application that 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.

Python Flask Tutorials


Previous Article
Next Article

Related Posts