Categories
Python Answers

How to get a variable from the URL in a Python Flask route?

Spread the love

Sometimes, we want to get a variable from the URL in a Python Flask route.

In this article, we’ll look at how to get a variable from the URL in a Python Flask route.

How to get a variable from the URL in a Python Flask route?

To get a variable from the URL in a Python Flask route, we can get it from the view function parameters.

For instance, we write

@app.route('/landingpage/<id>')
def landing_page(id):
    #...

to get the id URL parameter from the id parameter in the landing_page function.

We can use the url_for function to generate URLs for pages by writing

url_for('landing_page', id='A')

where the first argument is the view name.

Conclusion

To get a variable from the URL in a Python Flask route, we can get it from the view function parameters.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *