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 integer
   return '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()的更多相关文章

  1. Flask系列03--Flask的路由 app.route中的参数, 动态参数路由

    Flask–路由 添加路由的两种方式 第一种 @app.route("/my_de") def detail() 第二种(了解即可) app.add_url_rule(" ...

  2. 【转】Flask快速入门

    迫不及待要开始了吗?本页提供了一个很好的 Flask 介绍,并假定你已经安装好了 Flask.如果没有,请跳转到 安装 章节. 一个最小的应用 一个最小的 Flask 应用看起来会是这样: from ...

  3. FLASK简单入门

    假定你已经安装好了 Flask.如果没有,请跳转到 安装 章节. 一个最小的应用¶ 一个最小的 Flask 应用看起来会是这样: from flask import Flask app = Flask ...

  4. Flask框架入门

    Flask-基本入门 简介 flask被称为微型框架,只提供了一个强健的核心,其他功能全部通过扩展库来实现:也就是说可以根据项目需要量身打造.他适合入门学习以及高手研究. 组成:WSGI.模板引擎(J ...

  5. Flask 快速入门

    最简单的flask程序 from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return ...

  6. Flask从入门到精通之flask程序入门

    初始化 所有Flask程序都必须创建一个程序实例,Web服务器使用一种名为Web服务器网关接口的的协议(WSGI),把接收自客户端的所有请求转发给这个对象处理.程序实例是Flask类的对象,使用下面代 ...

  7. 二 Flask快速入门

    1: 外部可访问的服务器: 如果你运行了这个服务器,你会发现它只能从你自己的计算机上访问,网络中其它任何的地方都不能访问.在调试模式下,用户可以在你的计算机上执行任意 Python 代码.因此,这个行 ...

  8. Flask框架入门(一)

    Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架. Flask 本身相当于一个内核,其他几乎所有的功能都 ...

  9. Flask官方文档学习-flask快速入门

    环境搭建 下载安装Python3:www.python.org 终端运行命令:python3 -m venv flask_dev,来创建虚拟环境 启用虚拟环境,终端使用命令 source /flask ...

随机推荐

  1. “Hello World!”团队第二次会议

    今天是我们团队“hello world!”团队召开的第二次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 一.会议时间 20 ...

  2. Notes of the scrum meeting before publishing(12.19)

    meeting time:18:30~20:30p.m.,December 19th,2013 meeting place:3号公寓一层 attendees: 顾育豪                  ...

  3. Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  4. 自定义View 和 ViewGroup

    一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...

  5. 网众远程修改ip、dns

    修改文件 修改ip vi /etc/rc.d/rc.inetd1.config IPADDR[0] 对应第一块网卡的ip 修改dns vi /etc/resolv.conf nameserver 21 ...

  6. [WC2005]友好的生物

    description 洛谷 求 \[max_{1\le i<j\le n}\{\sum_{s=1}^{k-1}(C_s-|D_{is}-D_{js}|)-(C_k-|D_{ik}-D_{jk} ...

  7. 一条数据的HBase之旅,简明HBase入门教程-Write全流程

    如果将上篇内容理解为一个冗长的"铺垫",那么,从本文开始,剧情才开始正式展开.本文基于提供的样例数据,介绍了写数据的接口,RowKey定义,数据在客户端的组装,数据路由,打包分发, ...

  8. 【BZOJ1486】最小圈(分数规划)

    [BZOJ1486]最小圈(分数规划) 题面 BZOJ 洛谷 求图中边权和除以点数最小的环 题解 分数规划 二分答案之后将边权修改为边权减去二分值 检查有无负环即可 #include<iostr ...

  9. redux connect的浅比较说明

    redux的connect方法是一个高阶组件,对包装的组件会在ShouldComponentUpdate中实现一个默认的浅比较. connect形式如下: connect([mapStateToPro ...

  10. oracle-java7-installer安装java失败之后的处理

    最开始尝试使用installer安装jdk7,但是未能进行完整,之后每次安装软件都会报错,说oracle-java7-installer处有错误,查得如下解决办法: sudo rm /var/lib/ ...