Thursday, August 23, 2018

[Study] Flask url_for example

Why is this needed?

A YouTube video was the first time I encountered url_for feature. I couldn't understand what it was, for what it is. And today, I began to read the official manual

http://flask.pocoo.org/docs/0.12/quickstart/#url-building

And there are a lot of example code from opensource projects
https://www.programcreek.com/python/example/51519/flask.url_for


in short, url_for() function looks up app.route('/here')
if there are more than one app.route('/here'), the function looks up right above the function

test.py

from flask import Flask, url_for, render_template

app = Flask(__name__)

@app.route("/")
@app.route("/1st")
@app.route("/2nd")
@app.route("/3rd")
def lookAtMe():
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True)


index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    {{ url_for('lookAtMe') }}
  </body>
</html>

result


No comments:

Post a Comment

URDF Link origin test