Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables through URL to a flask app

Tags:

python

flask

Well i've this in my flask app :

@app.route("/changeip/<ip>")
def change_ip(ip) :
    return ip

Now if i invoke it like :

http://127.0.0.1:5000/changeip?ip=1.2.2.2

It spits out "URL not found"...what is that i'm doing wrong here?

like image 763
Aftnix Avatar asked Jun 05 '14 05:06

Aftnix


People also ask

How do you pass a variable in a URL in Flask?

If you are using the first route, the url should look like http://127.0.0.1/changeip/1.2.2.2 . If you are using the second url, the route should look like /changeip , the function should be def change_ip(): , and the value should be read from request. args['ip'] .

How do I link my URL to my Flask?

We can use the url_for() function to generate the links in the following way. Flask knows the mapping between URLs and functions so it will generate the /login and /profile URLs for us. This makes the application maintainable because if you want to change the URL, all you need to change the app.

How do you pass a variable in a link?

To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.


1 Answers

The first route describes a url with a value as part of the url. The second url describes a route with no variables, but with a query parameter in the url.

If you are using the first route, the url should look like http://127.0.0.1/changeip/1.2.2.2.

If you are using the second url, the route should look like /changeip, the function should be def change_ip():, and the value should be read from request.args['ip'].

Usually the route should describe any arguments that should always be present, and form or query params should be used for user-submitted data.

like image 122
davidism Avatar answered Oct 20 '22 00:10

davidism