Flask从入门到放弃1:路由app.route()
Flask从入门到放弃1:
Flask中的路由app.route():
参考来源:http://python.jobbole.com/80956/
https://www.raspberrypi.org/learning/python-web-server-with-flask/worksheet/
Flask是基于Werkzeug,Python WSGI实用程序库和Jinja2(Python的模板引擎)的微型框架。
比如:
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
要理解它,先了解一下装饰器:
举个例子:
# This is our decorator
def simple_decorator(f):
# This is the new function we're going to return
# This function will be used in place of our original definition
def wrapper():
print "Entering Function"
f()
print "Exited Function"
return wrapper
@simple_decorator
def hello():
print "Hello World"
hello()
运行上述代码会输出以下结果:
Entering Function
Hello World
Exited Function
上面是不接受传参的装饰器例子,下面演示一下传参的:
def decorator_factory(enter_message, exit_message):
# We're going to return this decorator
def simple_decorator(f):
def wrapper():
print enter_message
f()
print exit_message
return wrapper
return simple_decorator
@decorator_factory("Start", "End")
def hello():
print "Hello World"
hello()
给我们的输出是:
Start
Hello World
End
重新看一下前面的函数
@app.route("/"):
表示传递一个网站,“/”是网站的主目录,也就是http://127.0.0.1:5000/
假如把"/"改成:'/simon',那么就在网页上输入http://127.0.0.1:5000/simon
形参的规则可以用指定的转换器,比如下面的例子:
@app.route('/post/<int:post_id>')
def show_post(post_id):# show the post with the given id, the id is an integerreturn 'Post %d' % post_id
转换器有下面几种:
int:
接受整数
float:
同 int ,但是接受浮点数
path:
和默认的相似,但也接受斜线
def hello():
这个是传输给route的函数,里面返回值“Hello World!”就是显示到网页上的内容
假如需要显示html文件:
编写一个html文件,命名为index.html,比如:
<html>
<body>
<h2>Hello World</h2>
</body>
</html>
然后将return返回改成:
return render_template('index.html')
当然,在这之前要先导入 render_template模块
假如要导入CSS样式,编辑CSS文件,比如style.css:
body {
background: red;color: yellow;}
上述的html也做改动:
<html>
<head>
<link rel="stylesheet" href='/static/style.css' />
</head>
<body>
<h2>Hello World</h2>
</body>
</html>
整个项目的结构如下:
├── app.py
├── static
│ └── style.css
└── templates
└── index.html
我们还可以把导入模板,Flask使用jinja模板
@app.route('/hello/<name>')
def hello(name):
return render_template('page.html', name=name)
最后的return返回一个叫page.html的文件并传递形参name,name正是在URL的一部分数据
新建一个文件叫page.html
<h1>Hello {{ name }}!</h1>
这里我们忽略html的其他结构
网址输入:http://127.0.0.1:5000/hello/paul
我们就可以看到Hello paul的字样
Flask从入门到放弃1:路由app.route()的更多相关文章
- Flask系列03--Flask的路由 app.route中的参数, 动态参数路由
Flask–路由 添加路由的两种方式 第一种 @app.route("/my_de") def detail() 第二种(了解即可) app.add_url_rule(" ...
- 【转】Flask快速入门
迫不及待要开始了吗?本页提供了一个很好的 Flask 介绍,并假定你已经安装好了 Flask.如果没有,请跳转到 安装 章节. 一个最小的应用 一个最小的 Flask 应用看起来会是这样: from ...
- FLASK简单入门
假定你已经安装好了 Flask.如果没有,请跳转到 安装 章节. 一个最小的应用¶ 一个最小的 Flask 应用看起来会是这样: from flask import Flask app = Flask ...
- Flask框架入门
Flask-基本入门 简介 flask被称为微型框架,只提供了一个强健的核心,其他功能全部通过扩展库来实现:也就是说可以根据项目需要量身打造.他适合入门学习以及高手研究. 组成:WSGI.模板引擎(J ...
- Flask 快速入门
最简单的flask程序 from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return ...
- Flask从入门到精通之flask程序入门
初始化 所有Flask程序都必须创建一个程序实例,Web服务器使用一种名为Web服务器网关接口的的协议(WSGI),把接收自客户端的所有请求转发给这个对象处理.程序实例是Flask类的对象,使用下面代 ...
- 二 Flask快速入门
1: 外部可访问的服务器: 如果你运行了这个服务器,你会发现它只能从你自己的计算机上访问,网络中其它任何的地方都不能访问.在调试模式下,用户可以在你的计算机上执行任意 Python 代码.因此,这个行 ...
- Flask框架入门(一)
Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架. Flask 本身相当于一个内核,其他几乎所有的功能都 ...
- Flask官方文档学习-flask快速入门
环境搭建 下载安装Python3:www.python.org 终端运行命令:python3 -m venv flask_dev,来创建虚拟环境 启用虚拟环境,终端使用命令 source /flask ...
随机推荐
- Python中from module import *语法
from module import *的语法在Python 3.X和Python 2.X中的使用稍有区别: 在Python 3.X中,from module import *无法在函数里面使用,而在 ...
- python中argparse库的使用教程链接
这两篇文章详细介绍了argparse库的参数设置及使用包括位置参数与可选参数的用法 http://blog.csdn.net/guojuxia/article/details/44462381 htt ...
- DAY4敏捷冲刺
站立式会议 工作安排 (1)服务器配置 已完成对微信小程序登录凭证储存至云端数据库,计划使用微信接口返回的session_id进行转化返回本地,以保持登录态. (2)数据库配置 单词学习记录+用户信息 ...
- 向redis插入数据时,返回值问题
向redis插入数据时,如果redis没有要插入的key,插入成功之后返回值为1 如果redis有这个key,插入成功之后返回值是0
- LintCode-41.最大子数组
最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2 ...
- PHP利用pcntl_exec突破disable_functions
http://fuck.0day5.com/?p=563 PHP突破Disable_functions执行Linux命令 利用dl函数突破disable_functions执行命令 http://ww ...
- iOS开发自定义试图切换
CATransition *transition = [CATransition animation]; transition.duration = 1.0f; transition.timingFu ...
- OSPF学习中的问题
OSPF对接两方,对设置的要求,哪些参数必须相同 (HELLO &dead interval, area ID, authentation, 末节区域(option中的E位), network ...
- YaoLingJump开发者日志(一)
写完PokeCats之后意犹未尽,还想做一个更加有趣的游戏,比如说像超级玛丽那样. 游戏的主角就选"瑶玲"了,这是我小时候最喜欢的动画片<瑶玲啊瑶玲>的女主角. ...
- hadoop2.5.2学习及实践笔记(五)—— HDFS shell命令行常见操作
附:HDFS shell guide文档地址 http://hadoop.apache.org/docs/r2.5.2/hadoop-project-dist/hadoop-common/FileSy ...