【代码】Flask+Web开发:基于Python的Web应用开发实战

第2章 程序的基本结构

# encoding=utf-8
from flask import Flask, request, redirect, make_response
from flask_script import Manager


app = Flask(__name__)
# 添加命令行解析功能
manager = Manager(app)

@app.route('/')
def index():
	# request object
	user_agent = request.headers.get('User-Agent')
	return '

hello world!

\r\n

Your browser is {}

'.format(user_agent) # return response, status code # return '

bad request

',400 # redirect # return redirect('http://www.baidu.com') # make response # response = make_response('

This document carries a cookie!

') # response.set_cookie('answer', '42') # return response @app.route('/user/') def user(name): return '

hello, {}

'.format(name) @app.route('/user/') def get_user(id): user = load_user(id) if not user: abort(404) return '

hello, {}

'.format(user.name) if __name__ == '__main__': # app.run(debug=True) manager.run()

2.6 flask扩展

学到了扩展,Flask-Script别说使用,安装先喝了一壶

python2下 flask-script的情况

先看看书中做法:

安装方式
(venv) $ pip install flask-script
导入方式:
示例 2-3 hello.py:使用 Flask-Script
from flask.ext.script import Manager

环境:python 2.7.6,flask 1.0.2,所以应该不是版本的冲突吧?
报错:ImportError: No module named ext.script
更换导入方式:from flask_script import Manager
依然报错ImportError
deactivate 退出虚拟环境再source venv/bin/activate进入,然并卵

看到一贴:
no module named flask.ext.moment
在虚拟环境(取名:venv)下继续安装:

pip install Flask-Script(显示:Requirement already satisfied)
pip install Flask-Moment

依然报错:ImportError: No module named ext.script
更换导入方式:from flask_script import Manager
于是成功了吧?:

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()

optional arguments:
  -?, --help         show this help message and exit

python3下 flask-script的情况

后来使用python3的venv安装了虚拟环境(取名:py3)在py3环境下,导入报错,以上方法均不起作用,可见虚拟环境(py3)不被支持。。。
用python2的virtualenv安装虚拟环境(取名:py),在py环境下,运行python hello.py报错,运行python3 hello.py成功,显示:usage: hello.py [-?] {shell,runserver} …
不在虚拟环境py/py3下,运行python/python2/python3 hello.py均成功,所以说是虚拟环境的锅么。。。。。。

python3的安装,然并卵

默默去安装了3.6。。。Ubuntu怎样安装Python3.6,半路get到新技能apt-fast)
因为安装好了py3.6,于是在新建文件夹使拥venv开启虚拟环境,参考:关于Python3中venv虚拟环境
然而运行python3 -m venv .
报错:apt-get install python3-venv
于是安装,又报错:Unable to locate package python3-venv
运行sudo apt-get install python3.6-venv
又报错:python3.6-venv : Depends: python-pip-whl (>= 8.1.0-2) but 1.5.4-1ubuntu4 is

参考这里python3.6 部署后,又采用几种方法升级pip失败。。。。。。
最终把3.4的优先级改回去,在3.4下使用venv开启虚拟环境
那就先用着吧。。。。。
python虚拟环境配置pyenu在不同python版本之间轻松切换,同时参考pyenv-virtualenv插件虚拟出同一版本的python但是不同依赖库版本的开发环境。

ubuntu下环境配置目录/ect/bash.bashrc

第3章 模板

# encoding=utf-8
from flask import Flask, request, redirect, make_response
from flask import render_template
from flask_script import Manager
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from datetime import datetime


# 初始化
app = Flask(__name__)
bootstrap = Bootstrap(app)
moment = Moment(app)
# 添加命令行解析功能
# manager = Manager(app)

@app.route('/')
def index():
	return render_template('index.html', current_time=datetime.utcnow())

@app.route('/user/')
def user(name):
	return render_template('user.html', name = name)

@app.errorhandler(404)
def page_not_found(e):
	return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
	return render_template('500.html'), 500


if __name__ == '__main__':
	app.run(debug=True)
	# manager.run()

3.5 静态文件
书中两句:

示例 3-10 templates/base.html:定义收藏夹图标
{% block head %}
{{ super() }}


{% endblock %}

flask文档:
添加一个页面图标只有一句


只要templates目录下的某.html文件有:{% extends “base.html” %}
其对应页面即可在标签和收藏夹中显示图标

第4章 web表单

# encoding=utf-8
from flask import Flask, request, redirect, make_response
from flask import render_template, session, url_for, flash
from flask_script import Manager
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from datetime import datetime

# 定义表单类
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import Required

class NameForm(FlaskForm):
	name = StringField('what\'s your name?', validators=[Required()])
	submit = SubmitField('Submit')


# 初始化
app = Flask(__name__)
bootstrap = Bootstrap(app)
moment = Moment(app)
app.config['SECRET_KEY'] = 'hard to guess string'
# 添加命令行解析功能
# manager = Manager(app)

@app.route('/', methods=['GET', 'POST'])
def index():
	# name = None
	form = NameForm()
	if form.validate_on_submit():
		old_name = session.get('name')
		if old_name is not None and old_name != form.name.data:
			flash('Looks like you have changed your name')
		session['name'] = form.name.data
		return redirect(url_for('index'))
	return render_template('index.html', form=form, name=session.get('name'))

@app.route('/user/')
def user(name):
	return render_template('user.html', name = name)

@app.errorhandler(404)
def page_not_found(e):
	return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
	return render_template('500.html'), 500


if __name__ == '__main__':
	app.run(debug=True)
	# manager.run()

4.4 在视图函数中处理表单
发现注释不掉%{{ block content }}%,但可以注释掉普通html内容
在jinja2中注释是 {# #}

第5章

# encoding=utf-8
from flask import Flask, request, redirect, make_response
from flask import render_template, session, url_for, flash

from flask_script import Manager, Shell
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, MigrateCommand
from datetime import datetime
import os



# 定义表单类
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import Required

class NameForm(FlaskForm):
	name = StringField('what\'s your name?', validators=[Required()])
	submit = SubmitField('Submit')


# 初始化
# 配置数据库
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =\
 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
db = SQLAlchemy(app)


bootstrap = Bootstrap(app)
moment = Moment(app)
app.config['SECRET_KEY'] = 'hard to guess string'

# 添加命令行解析功能
manager = Manager(app)

# 为 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))
# 配置 Flask-Migrate
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)

# 定义 Role 和 User 模型
class Role(db.Model):
	 __tablename__ = 'roles'
	 id = db.Column(db.Integer, primary_key=True)
	 name = db.Column(db.String(64), unique=True)
	 users = db.relationship('User', backref='role', lazy='dynamic')

	 def __repr__(self):
	 	return '' % self.name
class User(db.Model):
	 __tablename__ = 'users'
	 id = db.Column(db.Integer, primary_key=True)
	 username = db.Column(db.String(64), unique=True, index=True)
	 role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
	 
	 def __repr__(self):
	 	return '' % self.username





# 视图函数
@app.route('/', methods=['GET', 'POST'])
def index():
	form = NameForm()
	if form.validate_on_submit():
		user = User.query.filter_by(username=form.name.data).first()
		if user is None:		
			user = User(username=form.name.data)
			db.session.add(user)
			session['known']=False
		else:
			session['known']=True
		session['name'] = form.name.data
		form.name.data=''
		return redirect(url_for('index'))
	return render_template('index.html', 
			form=form, name=session.get('name'), 
			known=session.get('known', False))

@app.route('/user/')
def user(name):
	return render_template('user.html', name = name)

@app.errorhandler(404)
def page_not_found(e):
	return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
	return render_template('500.html'), 500


if __name__ == '__main__':
	# app.run(debug=True)
	manager.run()

win下,不配置环境变量,初始化数据库迁移
【已解决】如何运行Flask-Migrate的db init

from app import db, app
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command("db", MigrateCommand)
if __name__ == "__main__":
    manager.run()

jinja2自定义过滤器
当前app的所有过滤器app.jinja_env.filters

通过改变jinja2环境变量(不建议)

# flask1.0.2, py3.6
from jinja2 import environment
def test(obj):
    return str(obj)
environment.DEFAULT_FILTERS["test"] = test
print(environment.DEFAULT_FILTERS)

直接给当前app添加过滤器,不会添加到jinjia2默认过滤器中

app.jinja_env.filters["test"]=test

通过添加方法添加(普通)

app.add_template_filter(test,"test")

通过装饰器添加(简单)

@app.template_filter("test")
def test(obj):
    return str(obj)

jinja2加载静态文件



创建表

app = Flask(import_name=__name__)  # type:Flask
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:[email protected]:3306/flask"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config['SQLALCHEMY_ECHO'] = True

db = SQLAlchemy(app)  # type:SQLAlchemy

class User(db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(length=80), nullable=False, unique=True, index=True)
    age = db.Column(db.Integer, nullable=False)
    email = db.Column(db.String(length=120), nullable=False, unique=True)

db.create_all()

根据app.config['SQLALCHEMY_ECHO'] = True查看sql语句

CREATE TABLE users (
	id INTEGER NOT NULL AUTO_INCREMENT, 
	username VARCHAR(80) NOT NULL, 
	age INTEGER NOT NULL, 
	email VARCHAR(120) NOT NULL, 
	PRIMARY KEY (id), 
	UNIQUE (email)
)

查看表创建sql语句,发现缺少了username的添加索引和唯一性约束,所以建表sql语句仅供参考。。

mysql> show create table users;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                          |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(80) NOT NULL,
  `age` int(11) NOT NULL,
  `email` varchar(120) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `ix_users_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

主键自增
orm创建字段不需要添加autoincrement=Trueid = db.Column(db.Integer, primary_key=True, autoincrement=True)
但是在原生sql语句是需要的id INTEGER NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),

执行执行sql语句
参考了Python中该怎么防止SQL注入

from sqlalchemy import text
users = db.session.execute(text("select * from users where username =:username"), {'username': "root"})

users = db.session.execute(
        text("select * from users where email like :con"),
        {"con": "%gmail%"})

users = db.session.execute(
                text("select * from users where username in :users"),
                {"users": ["root", "test", "roo"]}
            )

flask居然自己会缓存,清了浏览器缓存也没用:
app.config["TEMPLATES_AUTO_RELOAD"] = True app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 0

uwsgi_config.ini 配置文件

[uwsgi]
http = 0.0.0.0:80
processes = 4
threads = 2
master = true
pythonpath = /root/WechatTest
wsgi-file = /root/WechatTest/app.py
callable = app 
memory-report = true

你可能感兴趣的:(【代码】Flask+Web开发:基于Python的Web应用开发实战)