Flask學習 2修改路由規則 傳入參數訪問url
阿新 • • 發佈:2018-04-13
clas 命名參數 () author str auth 參數傳遞 deb use
#!/usr/bin/env python # encoding: utf-8 """ @version: v1.0 @author: cxa @file: flask02.py @time: 2018/04/13 14:55 """ """ 要給 URL 添加變量部分,你可以把這些特殊的字段標記為 <variable_name> , 這個部分將會作為命名參數傳遞到你的函數。規則可以用 <converter:variable_name> 指定一個可選的轉換器。這裏有一些不錯的例子: """ from flask import Flask app=Flask(__name__) htmlstr="""<html><head></head><body><div style="font-size:16px">this is my webpage,啟動app.run(<em style="color:red">debug</em>=True)以後可以隨時修改網頁的內容而不用重啟程序。</div></bodt></html>""" @app.route("/") def index(): return """<h1 style="align:center">Index Page </h1>""" @app.route("/hello") def hello(): return htmlstr @app.route(‘/user/<username>‘) def show_username(username): return "User %s"%username @app.route("/post/<int:post_id>") def show_post(post_id): """ int 接受整數 float 同 int ,但是接受浮點數 path 和默認的相似,但也接受斜線 :param post_id: :return: """ return "post %d" %post_id @app.route(‘/projects/‘) def projects(): """ 訪問一個結尾不帶斜線的 URL 會被 Flask 重定向到帶斜線的規範 URL 去 """ return ‘The project page‘ if __name__ == "__main__": app.run(debug=True)
Flask學習 2修改路由規則 傳入參數訪問url