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. 自测之Lesson4:gdb

    题目:列出gdb过程中常用的命令. 常用命令: 命令 作用 使用示例1 使用示例2 list 列出代码 list 行号 list 函数名 break 设置断点 break 行号 b 行号 run 运行 ...

  2. Hero In Maze(BFS广搜)

    Description 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了, ...

  3. 视频播放截图及简要文字介绍——Thunder团队

    视频播放截图及简要文字介绍 图一:团队Logo ——从此我们有了自己的标志 图二:扫描本地书籍 ——可阅读本地的喜爱书籍 图三:在本地添加自己喜爱的图书 ——将自己喜爱的书籍加入书架,方便阅读 图四: ...

  4. HDU 5794 A Simple Chess dp+Lucas

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 A Simple Chess Time Limit: 2000/1000 MS (Java/O ...

  5. SOA是什么为什么要面向服务编程

    SOA(面向服务的架构),Service-Oriented Architecture,面向服务的体系结构. 也就是以服务为核心的架构.这里需要理解什么是服务. 比如你有一个读取通知的方法: publi ...

  6. finecms

    finecms地址 还不错的国内CMS http://www.dayrui.com/doc/246.html

  7. LintCode-165.合并两个排序链表

    合并两个排序链表 将两个排序链表合并为一个新的排序链表 样例 给出 1->3->8->11->15->null,2->null, 返回 1->2->3- ...

  8. Bookmark Sentry – 检查重复、删除死链书签 Chrome扩展

    Bookmark Sentry 的用处,就是 处理重复的收藏夹的死链 . 重复链收藏.具体,请百度. Bookmark Sentry 下载 :  https://files.cnblogs.com/f ...

  9. Flink的序列化与flink-hadoop-compatibility

    最近 用户提交了一个问题 说他的jar包里明明包含相关的类型 但是在提交Flink作业的时候 却报出classnotfound的错误 查看之后发现 这里是flink的一个没有说的太明白的地方 用户的代 ...

  10. coreldraw x5提示盗版警告解决方法

    CorelDRAW是一款图形图像软件,大多数用户使用的都是coreldraw x5破解版,所以基本上都收到了coreldraw x5提示盗版警告,导致不能用,没关系,绿茶小编有解决方法. coreld ...