作者:hhh5460
官网有一个flaskr的例子,按照其8
个步骤(包括测试),一步一步照着做,有3
个地方报错。
究其原因,可能是flaskr这个例子年代比较久远,而现在python以及flask都有了很大的变动。
我的环境:win7(64) + python 3.5(64)
这里只直接贴出修改后的代码(出错的地方被注释了)。
其中flaskr.py
有2
处报错:
# flaskr.py
# 所有的导入
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
# 配置
# DATABASE = '/tmp/flaskr.db' # 报错1: sqlite3.OperationalError: unable to open database file
import os
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
DATABASE = os.path.join(PROJECT_ROOT, 'tmp', 'flaskr.db') # 改正1:不把路径写死
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
# 创建我们的小程序
app = Flask(__name__)
app.config.from_object(__name__) # from_object() 将会寻找给定的对象(如果它是一个字符串,则会导入它),
# 搜寻里面定义的全部大写的变量。
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
from contextlib import closing
def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql') as f:
#db.cursor().executescript(f.read()) # 报错2: ValueError: script argument must be unicode
db.cursor().executescript(f.read().decode()) # 改正2:加了decode()
db.commit()
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
g.db.close()
#四个视图函数:
# 1.显示条目
@app.route('/')
def show_entries():
cur = g.db.execute('select title, text from entries order by id desc')
entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
return render_template('show_entries.html', entries=entries)
# 2.添加新条目
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
g.db.execute('insert into entries (title, text) values (?, ?)',
[request.form['title'], request.form['text']])
g.db.commit()
flash('New entry was successfully posted')
return redirect(url_for('show_entries'))
# 3.登录
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_entries'))
return render_template('login.html', error=error)
# 4.注销
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('show_entries'))
if __name__ == '__main__':
app.run()
另,测试文件flaskr_test.py
有1
处报错:
import os
import flaskr
import unittest
import tempfile
class FlaskrTestCase(unittest.TestCase):
def setUp(self):
self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
flaskr.app.config['TESTING'] = True
self.app = flaskr.app.test_client()
flaskr.init_db()
def tearDown(self):
os.close(self.db_fd)
os.unlink(flaskr.app.config['DATABASE'])
# 测试1:访问应用程序的根目录(/)
def test_empty_db(self):
rv = self.app.get('/')
#assert 'No entries here so far' in rv.data # 报错3:TypeError: a bytes-like object is required, not 'str'
assert b'No entries here so far' in rv.data # 改正3:文字修改为byte类型(或者rv.data修改为rv.data.decode()),以下都要同样修改!!
def login(self, username, password):
return self.app.post('/login', data=dict(username=username, password=password), follow_redirects=True)
def logout(self):
return self.app.get('/logout', follow_redirects=True)
# 测试2:登录和注销
def test_login_logout(self):
rv = self.login('admin', 'default')
assert b'You were logged in' in rv.data
rv = self.logout()
assert b'You were logged out' in rv.data
rv = self.login('adminx', 'default')
assert b'Invalid username' in rv.data
rv = self.login('admin', 'defaultx')
assert b'Invalid password' in rv.data
# 测试3:添加新条目
def test_messages(self):
self.login('admin', 'default')
rv = self.app.post('/add', data=dict(title='<Hello>', text='<strong>HTML</strong> allowed here'), follow_redirects=True)
assert b'No entries here so far' not in rv.data
assert b'<Hello>' in rv.data
assert b'<strong>HTML</strong> allowed here' in rv.data
if __name__ == '__main__':
unittest.main()