【Flask notes】url_for()

flask notes: url_for

url_for in Flask is used for creating a URL to prevent the overhead of having to change URLs throughout an application (including in templates). Without url_for, if there is a change in the root URL of your app then you have to change it in every page where the link is present.

example:

@app.route('/index')
@app.route('/')
def index():
    return 'you are in the index page'

Now if you have a link the index page:you can use this:

<a href={{ url_for('index') }}>Index</a>

example 2:

@app.route('/questions/<int:question_id>'):  
def find_question(question_id):  
    return ('you asked for question{0}'.format(question_id))

For the above we can use:

<a href = {{ url_for('find_question' ,question_id=1) }}>Question 1</a>

Like this you can simply pass the parameters!

example 3:

<script src="{{ url_for('static', filename='jquery.min.js') }}"></script>

<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">

ref:
source

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,066评论 0 23
  • 黑白摄影
    闷漏阅读 376评论 0 1
  • JanusGraph中文翻译文档 JanusGraph设计的目的是处理大图,单机无论是在存储和计算能力上都无法满足...
    CodeYangX阅读 1,831评论 0 0
  • 1. 参与 就个人而言,不想参与事情分两种,一是真的不喜欢,二是不擅长,害怕失败而不去参与来避免出丑。由此想到,能...
    平行的稻草阅读 304评论 0 0