2016年9月6日 星期二

[Flask] static file的存取

參考這邊:
http://docs.jinkan.org/docs/flask/quickstart.html#routing

在route規則中可以這樣寫

@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username
網頁url/user/username  這個username的字會被username變數接收
所以打入url/user/malo  --> username就會是malo

另一種寫法,把post_id轉為int型態
<converter:variable_name>
@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id
中文的資料只有說3種converter
但在這邊,寫了好幾個converter
http://flask.pocoo.org/docs/0.11/quickstart/#variable-rules
string和path主要分別在於path可以包含slash
@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username

@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id
The following converters exist:
stringaccepts any text without a slash (the default)
intaccepts integers
floatlike int but for floating point values
pathlike the default but also accepts slashes
anymatches one of the items provided
uuidaccepts UUID strings



沒有留言:

張貼留言