Flask學習 2修改路由規則 傳入參數訪問url


#!/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)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM