from flask import Flask
app = Flask(__name__)
#
# 路由用法
#
@app.route('/')
def hello_world():
# 这种方法再不传参的情况下可以使用,一般用在首页
# 导航栏:http://127.0.0.1:5000
return 'hello world!!!' @app.route('/hello')
def hello_world1():
# 导航栏访问:http://127.0.0.1:5000/hello
return 'hello world!!!' @app.route('/hello/<name>')
def hello_world2(name):
# 这里可以传递一个参数
# 导航栏访问:http://127.0.0.1:5000/hello/namejr
return '{}, hello'.format(name) @app.route('/hello/<int:num>')
def hello(num):
# 可以指定参数的类型
# 导航栏访问:http://127.0.0.1:5000/hello/88
# 除了可以指定int(整形)之外,还可以指定:string(字符串,但值得注意的是不能够包含'/')、float(浮点型)、path(路径,与string的区别是这里可以包含'/')、uuid(uuid的字符串)
# UUID是一种唯一识别码,其目的是让分布式系统中的所有元素,都能有唯一的辨识信息,而不需要通过中央控制端来做辨识信息的指定。(来源百度百科)
return '{} is a number'.format(num)

关于url_for:

from flask import Flask, url_for
app = Flask(__name__)
#
# 关于url_for
# url_for用来可以直接获取对应函数名的路由路径
# 这样的好处就是不管你如何更改路径,只要函数名没有改变,使用url_for都能够直接的获取对应的路径
#
@app.route('/')
def index():
return url_for('hello') # 打印"/hello" @app.route('/hello')
def hello():
return "hello True" if __name__ == '__main__':
app.debug = True
app.run()

我们可以使用test_request_content(test_request_content是一个用服务器来模拟用户发送请求的函数,实际上并没有用户发送数据)来测试一下

from flask import Flask, url_for
app = Flask(__name__)
#
# 关于url_for
# url_for用来可以直接获取对应函数名的路由路径
# 这样的好处就是不管你如何更改路径,只要函数名没有改变,使用url_for都能够直接的获取对应的路径
#
@app.route('/')
def index():
return url_for('hello') # 打印"/hello" @app.route('/hello/')
def hello():
return "hello True" # 构建模拟情景
with app.test_request_context():
print(url_for('index')) # 打印"/"
print(url_for('hello')) # 打印"/hello/" if __name__ == '__main__':
app.debug = True
app.run()

顺带一提:在编写路由的时候,你可以写成"@app.route('/hello/')",当然也可以写成"@app.route('/hello')",但是我最终建议还是写成前者,如果你访问的是"http://127.0.0.1:5000/hello",那么都可以访问,但是如果访问的是"http://127.0.0.1:5000/hello/"那么后者将会报出404("Not Found")错误,不信你试试,反正我试过了

PS:路由,不就是装饰器嘛

HTTP

路由和HTTP方法的更多相关文章

  1. mvc中多参数URL会很长,首次加载不传参数让url很短,路由规则实现方法[bubuko.com]

    如要实现列表中地址全路径“bubuko-11-2.html”,在首次进入时,使用短路径“bubuko.html”,只有再次href后才显示全路径“bubuko-11-2.html”,下面使用路由规则来 ...

  2. 建立tracert路由列表的方法

    建立tracert路由列表的方法:电脑屏幕左下方 选择开始选项运行 输入 CMD在DOS命令行下输入:tracert (你的网站域名)   运行结果中如出现了“*     *     *    req ...

  3. centos7 防火墙一些相关设置 开机添加静态路由 特殊的方法

    参考文献: https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/security_guide/s ...

  4. Redhat、CentOS添加静态路由的标准方法

    我们经常遇到需要在系统默认路由的基础上,额外添加静态路由的需求.为了使得下次系统启动这些静态路由依旧生效,我们可能采取在rc.loal里加入route命令追加静态路由的方法. 现在给大家推荐Redha ...

  5. 基于hi-nginx的web开发(python篇)——动态路由和请求方法

    hi.py的提供的路由装饰器接受两个参数,第一个参数指定动态路由的正则模式,第二个参数指定同意的http请求方法列表. 比如: @app.route(r"^/client/?$", ...

  6. 关于linux的添加永久静态路由的static-routes方法

    一:使用 route 命令添加 使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法: //添加到主机的路由 # route add –host 192.168.1.11 dev ...

  7. angular路由传参和获取路由参数的方法

    1.首先是需要导入的模块 import { Router } from "@angular/router";//路由传参用到 import{ActivatedRoute,Param ...

  8. 使用ASP.NET Core 3.x 构建 RESTful API - 3.2 路由和HTTP方法

    ASP.NET Core 3.x 的路由 路由机制会把一个请求的URI映射到一个Controller上面的Action,所以当你发送一个HTTP请求的时候,MVC框架会解析这个请求的URI,并尝试着把 ...

  9. Flutter中管理路由栈的方法和应用

    原文地址:https://www.jianshu.com/p/5df089d360e4 本文首先讲的Flutter中的路由,然后主要讲下Flutter中栈管理的几种方法. 了解下Route和Navig ...

随机推荐

  1. Delphi 10.3最新消息

    因为Google Play的要求,使Delphi官方被动的透出点消息:https://community.embarcadero.com/blogs/entry/deadline-approachin ...

  2. http协议与websocket协议(转)

    一.WebSocket是HTML5中的协议,支持持久连接:而Http协议不支持持久连接. 首先HTMl5指的是一系列新的API,或者说新规范,新技术.WebSocket是HTML5中新协议.新API. ...

  3. vmware安装centos问题

    * 选择安装后,一直黑屏 重新启动电脑,在启动界面进入BIOS设置,找到Intel (R) Virtualization Technology,设置成Enabled,保存设置以后重新启动电脑,问题解决 ...

  4. Skflow mac安装 for tensorflow-0.8.0

    参考: # Mac OS X, CPU only: $ pip install --ignore-installed --upgrade https://storage.googleapis.com/ ...

  5. 打开视图 :1449 - the user specified as a definer ('root'@'%')does not exist

    从一个数据库数据迁移到本地localhost   程序在调用到数据库的视图时报错,直接在数据库中打开视图时也报错,类似:   mysql 1449 : The user specified as a ...

  6. Light OJ 1199:Partitioning Game(SG函数模板)

    Alice and Bob are playing a strange game. The rules of the game are: 1.      Initially there are n p ...

  7. tomcat部署成https协议

    1 生成密匙:进入jdk的bin目录后输入: keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keysto ...

  8. 2018.4.23 git命令总结

    git clone git pull git add xx git add . git add -A git branch 查看本地分支 git branch --all 查看所有分支 git bra ...

  9. hdu1686 Oulipo KMP/AC自动机

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  10. CTEX(LaTeX) 编译 中文

    CTEX 中文编码&编译问题 #win10 tex 文档为 GBK 编码 https://zhidao.baidu.com/question/93645685.html \documentcl ...