Flask学习笔记:javascript
GitHub上面的Flask实践项目css
https://github.com/SilentCC/FlaskWeb
html
1.Application and Request Context(上下文)前端
在Flask 中,通常一个view function(视图函数)会处理一个请求
Flask 中提供request context.保证全局只有一个线程的request,而不会同时出现两个request.
Application and Request Context 一共有四种
current_app (Application context)
g (Application context)
request (Request context)
session (Request context)
2.Request Dispatching(请求发送)
当服务器接受一个请求,Flask经过创建一个URL mapping ,为请求找到相应的视图函数。
Flast 利用 app.route 创建这个Map.
>>> from hello import app
>>> app.url_map
Map([<Rule '/' (HEAD, OPTIONS, GET) -> index>,
<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,
<Rule '/user/<name>' (HEAD, OPTIONS, GET) -> user>])
其中 /static/<filename> route 是Flask中特有的。
3.Request Hooks(请求挂钩)
在接受请求或者处理完请求以后,都要执行一段代码。好比请求发送以前,咱们须要链接一下数据库。因此
咱们能够写一个链接数据库的通用函数。Flask 中Request hook function 提供了这个功能
有四种hook 函数
before_first_request: 当接受第一个请求以前要执行的代码。
before_request :接受每个请求以前要执行的代码。
after_request:处理完每个请求以后要执行的代码,只有请求成功处理以后。
teardown_request:处理完每个请求以后要执行的代码,请求处理失败以后也能够执行的。
在 request hook function 和view function 之间的数据共享是经过全局的g context来完成的。
例如登陆以前,经过before_request请求链接数据库的获取用户的信息 g.username。而后在
view function 中就能够调用g.username
4.Responses(回应)
每一个view function 都会返回一个value。能够返回html 页面。
可是http 协议要求咱们还须要返回一个状态,好比200 就是成功执行请求,400 就是执行请求发生错误。
for example
app.route('/')
def index():
return '<h1>error request<h1>,400'
view function 能够返回两个,也能够返回三个(value,status,headers)
Flask 也提供了专门一个函数make_response()
@app.route('/')
def index2():
response=make_response('<h1>This document carries a cookie!</h1>')
response.set_cookie('answer','42')
return response
除此以外,还有两种response方式:
redirect(重定向) 实际上一个返回三个的response
from flask import redirect
@app.route('/')
def index():
return redirect('http://www.example.com')
abort(用于抛出错误)
from flask import abort
@app.route('/user/<id>')
def get_user(id):
user = load_user(id)
if not user:
abort(404)
return '<h1>Hello, %s</h1>' % user.name
5.Extensions
Command-LineOptions with Flask-Script
Flask 具备可扩展性,能够下载不少插件。
好比Flask-script 这个插件,就是可让Flask使用命令行在后台。
举个列子:
首先下载Flask_script: $ pip install flask-script
要使用Command-LineOptions这个插件,那么hello.py就要作相应的改变。
而后咱们在hello.py中能够这么写:
#!/bin/python
from flask import Flask,make_response
from flask.ext.script import Manager
app = Flask(__name__)
manager = Manager(app)
#...
if __name__ =='__main__':
manager.run()
这个时候在终端,咱们就能够用命令。
$ python hello.py
usage: hello.py [-?] {shell,runserver} ...
positional arguments:
{shell,runserver}
shell Runs a Python shell inside Flask application context.
runserver Runs the Flask development server i.e. app.run()
其中shell 命令是启动application 中的context的session 前面提到过。
runserver命令是启动Web server的。
在runserver命令中咱们能够查看不少命令,$ python hello.py runserver --help
其中有-h,-t,--host,-p,--post 等等
--host能够设置服务器监听的网络地址。--host 0.0.0.0 全部的地址均可以访问服务器。
6.Jinja2 模板引擎。
首先咱们理解Jinja2 模板引擎是个什么东西。其实Jinja 模板引擎就是在html的基础上,在须要交互
数据的地方作一些标注,能实现先后端数据交互。这样就省了不少前端传到后端的代码,实现先后端的MVC
,方便开发。Flask 使用Jinja2模板引擎。
Jinja2 模板引擎之 变量
打印变量用{{ Varibles }}
例如:{{ list[0] }} 打印list数组里的第一个元素。
变量的过滤器,咱们能够在变量后面加一个过滤器,对变量进行操做.过滤器和变量用|分隔。
例如:{{ name|capitalize}} 把name这个变量的首字母大写。
下面介绍几个过滤器:
Filter name Description
safe Renders the value without applying escaping
capitalize Converts the first character of the value to uppercase and the rest to lowercase Converts the value to lowercase characters
lower ase Converts the value to lowercase characters
upper Converts the value to uppercase characters
title Capitalizes each word in the value
trim Removes leading and trailing whitespace from the value
striptags Removes any HTML tags from the value before rendering
其中safe 过滤器的做用是关闭html转义。还有escapes过滤器是html转义。好比一个字符串 s='<h1>hello world</h1>'
{{s|escapes}} 则是将这个字符串转移成html的内容,从而显示<h1>标签的hello world。
若是{{s|safe}} 那就是打印<h1>hello world<h1>
执行语句 用{% %}
if条件
{% if user %}
Hello,{{ user }}!
{% else %}
Hello,Stranger!
{% endif %}
for循环
<ul>
{% for comment int comments %}
<li>{{comment}}</li>
{% endfor %}
</ul>
宏定义 macro
{% macro render_comment(comment)%}
<li>{{comment}}</li>
{% endmacro %}
<ul>
{% for comment in comments %}
{{ render_comment(comment) }}
{% endfor %}
</ul>
宏定义 macro 还能够导入文件,例如将上面的
{% macro render_comment(comment)%}
<li>{{comment}}</li>
{% endmacro %}
放到macros.html里面,再经过导入的方式使用宏定义
{% import 'macros.html' as macros %}
<ul>
{% for comment in comments %}
{{ macros.render_comment(comment) }}
{% endfor %}
</ul>
若是有多处须要用到重复的代码块,能够统一到一个文件里,经过include
{% include 'common.html' %}
flask还有一个很重要的用法就是继承性。咱们能够写一个模板网页base.html
<html>
<head>
{% block head %}
<title>{% block title %}{% endblock %} - My Application</title> {% endblock %}
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
咱们再写一个base2.html来继承base.html
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
<style>
</style>
{% endblock %}
{% block body %} <h1>Hello, World!</h1> {% endblock %}
这里extends就是继承的标志,
block标签:被block标记的代码块,是能够被改变的
在子页面中,能够新建一个block,也能够对父页面中的block进行重写
super()则是继承父页面中的block中的已有的代码
7.Bootstrap
Flask中也可使用boostrap,Flask提供了Flask-Bootstrap 扩展包,用来将
使用bootstrap
$ pip install flask-bootstrap 安装flask-bootstrap
from flask.ext.bootstrap import Bootstrap
# ...
bootstrap = Bootstrap(app) 初始化flask-bootstrap。
8.连接
网页中有不少须要经过url跳转到其余页面的地方,若是是一个固定的URL,就会很简单,可是若是是一个动态的URL
好比带参数的URL,就会比较麻烦。Flask提供了url_for()函数,来统一管理这些URL
url_for最简单的用法,以hello.py中的视图函数名为参数,则会返回这个视图函数的URL
例如:
视图函数:
@app.route('/user/<name>')
def user(name):
return render_template('user.html',name=name)
url_for('user', name='john', _external=True)则会返回
http://localhost:5000/user/john.
url_for也能够不用视图函数里的参数,用本身定义的例如:
url_for('index', page=2) 会返回 /?page=2.
url_for也能够引用不少固定文件,好比css文件,javascript文件,图片等等
这些固定文件,通常都放在static文件夹下
url_for('static', filename='css/styles.css', _external=True)
会返回 http://localhost:5000/static/css/styles.css.
9.日期和时间
Flask提供了flask-moment,依赖于javascript中的jquery.js和moment.js
提供了对时间和日期处理的各类函数
$ pip install flask-moment 安装flask-moment
初始化moment
from flask.ext.moment import Moment
moment = Moment(app)
在网页中导入moment.js
{% block scripts %}
{{ moment.include_moment() }}
{% endblock %}
10.表格
在Flask中使用表格,Flask提供了flask-wtf
$ pip install flask-wtf 安装flask-wtf
flask-wtf 保护网站的避免受到CSRF攻击,所谓CSRF攻击,就是用户在浏览一个网站的时候,得到该网站的
安全验证,此时在这个网页中点击了另外一个恶意网站,恶意网站就能够得到用户的信息,操控用户作一些用户不想作的事情
在hello.py中加入密钥,防止CSRG
pp = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
SECRET_KEY是密钥,配置密钥的字符串本身能够设置一个,至关于密码
使用表格的时候,flask是将表格在hello.py做为一个类,而后前端再调取这个form Classes
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField from wtforms.validators import Required
class NameForm(Form):
name = StringField('What is your name?', validators=[Required()])
submit = SubmitField('Submit')
前端界面:
<form method="POST">
{{ form.name.label }} {{ form.name() }}
{{ form.submit() }}
</form>
用flask-wtf和flask-bootstrap得到一个表格
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Flasky{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1>
</div>
{{ wtf.quick_form(form) }}
{% endblock %}
bootstrap/wtf.html 定义了函数,提供用Bootstrap的Flask-WTF
11.session
用法和jsp的session相似
from flask import session
12.flash
若是要在网页中弹出提示框,Flask提供了flash
from flask import flash
flash('error')
相应的在前端界面:
{{ get_flashed_messages() }}显示flash里面的内容
若是有不少条flash须要显示
就能够用for循环
{% for message in get_flashed_messages() %}
{{ get_flashed_messages() }}
{% endfor %}
14.整合python shell
def make_shell_context():
return dict(app=app, db=db, User=User, Role=Role) manager.add_command("shell", Shell(make_context=make_shell_context))
maker_shell_context()函数,注册了程序和数据库,已经实体,而后就能够控制台使用shell命令使用
$ python hello.py shell
>>> app
<Flask 'app'>
>>> db
<SQLAlchemy engine='sqlite:////home/flask/flasky/data.sqlite'> >>> User
<class 'app.User'>
13.数据库
14.Mailjava