Passing variables through URL to a flask app

10,229

Solution 1

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.

Solution 2

You should use:

app.route('/something/<ip>')
def function(ip):

And when you are using url_for, you should pass value of ip aswell:

url_for('function', ip='your_ip_address')
Share:
10,229

Related videos on Youtube

Aftnix
Author by

Aftnix

Passionate about computers,physics and mathematics. I guess that's about it. My carrier 2.0 Profile : http://careers.stackoverflow.com/cv/edit/104377

Updated on June 04, 2022

Comments

  • Aftnix
    Aftnix almost 2 years

    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?

  • G M
    G M about 3 years
    I think you should import request from Flask, not the module requests