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. 【Linux】- vi/vim

    所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正 ...

  2. java 基础--数组--004

    1,数组定义格式 String[] aa; String aa[];2,二位数组的定义格式1 String aa[][] = new String[m][n]; String[] aa[] = new ...

  3. python 爬虫每天定时启动爬虫任务

     # coding=utf-8 import datetime import time def doSth(): # 这里是执行爬虫的main程序     print '爬虫要开始运转了....'   ...

  4. web传参

    页面通过对象,将表单数据传送给后端,后端通过对象接收参数值,

  5. 【其他】VS提示不一致的行尾

    应该是用不同的编辑器或平台编辑过同一个文件,比如Windows是\r\n,有的系统只有一个\n, 需要都统一,否则代码可能会堆成一堆.

  6. 将下载到本地的JAR包手动添加到Maven仓库(转)

    常用Maven仓库网址:http://mvnrepository.com/http://search.maven.org/http://repository.sonatype.org/content/ ...

  7. Matlab中save与load函数的使用

    用save函数,可以将工作空间的变量保存成txt文件或mat文件等. 比如: save peng.mat p j 就是将工作空间中的p和j变量保存在peng.mat中. 用load函数,可以将数据读入 ...

  8. 单选 name的值相同时候 就会产生互斥现象

  9. 【bzoj1798】[Ahoi2009]Seq 维护序列seq 线段树

    题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一 ...

  10. git log 查看提交记录

    git log 查看提交记录 1. git log 查看提交历史记录2. git log --oneline 或者 git log --pretty=oneline 以精简模式显示3. git log ...