博客
关于我
Flask简单学习
阅读量:431 次
发布时间:2019-03-06

本文共 2919 字,大约阅读时间需要 9 分钟。

一:安装

直接 pip install Flask,就可以安装好了

二:hello world

编写一个hello.py

# coding: utf-8from flask import Flaskapp = Flask(__name__)@app.route('/')def hello_world():    return 'Hello world'if __name__ == '__main__':    app.run()

 

然后在python解析器下运行

$ python hello.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
在浏览器上访问 http://127.0.0.1:5000/
然后就会出现 Hello world

1:上面有一个route的装饰器告诉Flask什么样的URL访问能触发这个函数

2: app.run() 这个也可以绑定具体的host和port

三:路由

如上面的程序,route() 装饰器把一个函数绑定到了对应的URL上了

基本用法:

# coding: utf-8from flask import Flaskapp = Flask(__name__)@app.route('/')def hello_wold():    return 'Hello world'if __name__ == '__main__':    app.run()

 

变量规则:

要给 URL 添加变量部分,你可以把这些特殊的字段标记为 <variable_name> , 这个部分将会作为命名参数传递到你的函数。规则可以用 <converter:variable_name> 指定一个可选的转换器

# coding: utf-8@app.route('/user/
')def show_user_profile(username): # show the user profile for that user return 'User %s' % username@app.route('/post/
')def show_post(post_id): # show the post with the given id, the id is an integer return 'Post %d' % post_id

 

构造URL:

编写一个名为 cons_url.py的程序

# coding: utf-8from flask import Flask, url_forapp = Flask(__name__)@app.route('/')def index(): pass@app.route('/login')def login(): pass@app.route('/user/
')def profile(username): passwith app.test_request_context(): print url_for('index') print url_for('login') print url_for('login', next='/') print url_for('profile', username='John Doe')

在命令行下运行程序,输出

$ python cons_url.py

/
/login
/login?next=%2F
/user/John%20Doe

 

HTTP方法:

默认情况下,路由只回应 GET 请求,但是通过 route() 装饰器传递 methods 参数可以改变这个行为

@app.route('/login', methods=['GET', 'POST'])def login():    if request.method == 'POST':        do_the_login()    else:        show_the_login_form()

四:模板渲染

Flask 配备了 Jinja2 模板引擎

你可以使用 render_template() 方法来渲染模板

# coding: utf-8from flask import render_template@app.route('/hello/')@app.route('/hello/
')def hello(name=None): return render_template('hello.html', name=name)

 

Flask 会在 templates 文件夹里寻找模板。所以,如果你的应用是个模块,这个文件夹应该与模块同级;如果它是一个包,那么这个文件夹作为包的子目录:

情况 1: 模块:/application.py/templates    /hello.html情况 2: 包:/application    /__init__.py    /templates        /hello.html

 

更多模板请到:http://docs.jinkan.org/docs/jinja2/

五:重定向:

# coding:utf-8from flask import Flask, redirect, url_forapp = Flask(__name__)@app.route('/index/', methods=['GET', 'POST'])def index():    # return redirect('/login/')    return redirect(url_for('login'))@app.route('/login/', methods=['GET', 'POST'])def login():    return "LOGIN"app.run()

用  redirect() 函数来进行url重定向

六:定义错误页面:

# coding:utf-8
from flask import Flask, abort, render_templateapp = Flask(__name__)@app.route('/index/', methods=['GET', 'POST'])def index():    return "OK"@app.errorhandler(404)def page_not_found(error):    return render_template('page_not_found.html'), 404app.run()

用@app.errorhandler(http错误码) 来触发一个函数

 

参考:

   http://docs.jinkan.org/docs/flask/

   http://flask.pocoo.org/

转载地址:http://dyfyz.baihongyu.com/

你可能感兴趣的文章
multi_index_container
查看>>
mutiplemap 总结
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>