Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python flask how to get route id from url

Tags:

I am using flask. I have a url such as: http://example.com/page/page_id

I want to know how to get the page_id part from url in the route. I am hoping I could devise some method such as:

@route('/page/page_id')    def page(page_id):        pageid = page_id 
like image 535
ashutosh Avatar asked Jan 30 '15 05:01

ashutosh


People also ask

How can I get the named parameters from a URL using Flask?

In the first one we would use request. args. get('<argument name>') where request is the instance of the class request imported from Flask. Args is the module under which the module GET is present which will enable the retrieve of the parameters.

What is URL routing in Flask?

App routing is used to map the specific URL with the associated function that is intended to perform some task. It is used to access some particular page like Flask Tutorial in the web application.

What is url_for in Flask?

The url_for() function is very useful for dynamically building a URL for a specific function. The function accepts the name of a function as first argument, and one or more keyword arguments, each corresponding to the variable part of URL.


Video Answer


1 Answers

It's pretty straightforward - pass the path parameter in between angle brackets, but be sure to pass that name to your method.

@app.route('/page/<page_id>') def page(page_id):     pageid = page_id     # You might want to return some sort of response... 
like image 144
Makoto Avatar answered Oct 21 '22 07:10

Makoto