0

Is it possible to use a URL variable inside the function in flask? I have searched extensively and have not gotten any wiser.

If I display pathVariable in the html template it displays whatever I would type in.

from flask import Flask, request
app = Flask(__name__)

@app.route('/<pathVariable>/')
def test(pathVariable=pathVariable):

    test = pathVariable
    path = request.path
    script_root = request.script_root
    base_url = request.base_url
    url = request.url
    url_root = request.url_root
    url_rule = request.url_rule

    print ("test is: %s" %path)
    print ("path is: %s" %path)
    print ("script_root is: %s" %script_root)
    print ("base_url is: %s" %base_url)
    print ("url is: %s" %url)
    print ("url_root is: %s" %url_root)
    print ("url_rule is: %s" %url_rule)

    return render_template('/example.html', pathVariable=pathVariable)

if __name__ == '__main__':
    app.run()

For example if I type in :

127.0.0.1:5000/tryone and 127.0.0.1:5000/trytwo, the pathVariable would be rendered as tryone or trytwo within the html template.

But inside the test function the printout I get is:

test is: /favicon.ico/
path is: /favicon.ico/
script_root is: 
base_url is: http://http://127.0.0.1:5000/favicon.ico/
url is: http://http://127.0.0.1:5000/favicon.ico/
url_root is: http://http://127.0.0.1:5000/
url_rule is: /<pathVariable >/

I there any way that I could get tryone or trytwo inside the test function? I have a dictionary with lists associated to each of these variables, and they determine what variables must be returned (rendered) back to the html template.

2
  • 1
    you seem to be trying to define the pathvariable in the function def test(pathVariable=pathVariable): - should this not simply be def test(pathVariable): Commented Jan 28, 2016 at 13:40
  • 1
    Thank you Craicerjack! I read your comment and was just testing it before I would comment and confirm, then you beat me to it. Once again this answered my question, thank you! Commented Jan 28, 2016 at 13:59

1 Answer 1

1

You are defining the pathVariable in the function which seems to be the issue. Changing your code to this:

from flask import Flask, request
app = Flask(__name__)

@app.route('/<pathVariable>/')
def test(pathVariable):

    test = pathVariable
    path = request.path
    script_root = request.script_root
    base_url = request.base_url
    url = request.url
    url_root = request.url_root
    url_rule = request.url_rule

    print ("test is: %s" %path)
    print ("path is: %s" %path)
    print ("script_root is: %s" %script_root)
    print ("base_url is: %s" %base_url)
    print ("url is: %s" %url)
    print ("url_root is: %s" %url_root)
    print ("url_rule is: %s" %url_rule)

    return render_template('/example.html', pathVariable=pathVariable)

if __name__ == '__main__':
    app.run()

prints out

127.0.0.1 - - [28/Jan/2016 13:49:02] "GET /trytwo HTTP/1.1" 301 -
test is: /trytwo/
path is: /trytwo/
script_root is: 
base_url is: http://localhost:5000/trytwo/
url is: http://localhost:5000/trytwo/
url_root is: http://localhost:5000/
url_rule is: /<pathVariable>/
127.0.0.1 - - [28/Jan/2016 13:49:02] "GET /trytwo/ HTTP/1.1" 500 -
test is: /favicon.ico/
path is: /favicon.ico/
script_root is: 
base_url is: http://localhost:5000/favicon.ico/
url is: http://localhost:5000/favicon.ico/
url_root is: http://localhost:5000/
url_rule is: /<pathVariable>/
127.0.0.1 - - [28/Jan/2016 13:49:02] "GET /favicon.ico/ HTTP/1.1" 500 -

Also all webpages will try to find the favicon for the site so the code showing isnt unusual.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.