美文网首页
【Flask notes】url_for()

【Flask notes】url_for()

作者: 爱睡的蟹老板 | 来源:发表于2019-03-02 11:28 被阅读0次

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

相关文章

网友评论

      本文标题:【Flask notes】url_for()

      本文链接:https://www.haomeiwen.com/subject/jikeuqtx.html