比利牛斯獒犬 flask web

1.

使用 session.get('name') 直接从会话中读
取 name 参数的值。和普通的字典一样,这里使用 get() 获取字典中键对应的值以避免未找
到键的异常情况,因为对于不存在的键, get() 会返回默认值 None 。

2.数据库迁移

使用shell 注册新用户,commit() 时出错,users 表没有“email”列

 使用db init 创建迁移仓库,此命令会创建 migrations文件夹

migrate 子命令用来自动创建迁移脚本

使用 db upgrade 把迁移 应用到数据库中

 3.新增页面流程:

表单:form.py

显示这个表单的模板:register.html

路由:views.py

具体功能实现:model.py

4.python编码

password = PasswordField('password', validators=[
Required(), EqualTo('password2', message='Passwords must match.')])

将 ‘password’换成 ‘密码’报错:

Python报错:unicodedecodeerror ascii codec can t decode byte 0xe5 in position 0 ordinal not in range 128  

Python编码是 unicode -> str,解码就是 str -> unicode。
如果在Python文件中写入 # -*- coding:utf-8 -*- ,那么就使得Python 脚本文件是 UTF-8 编码的。

http://blog.163.com/xh_ding/blog/static/1939032892014119105749373/

5.test

test_password_setter (test_user_model.UserModelTestCase) ... ok
test_password_verification (test_user_model.UserModelTestCase) ... ok
test_valid_confirmation_token (test_user_model.UserModelTestCase) ... FAI
L

======================================================================
FAIL: test_valid_confirmation_token (test_user_model.UserModelTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "E:userdataPycharmProjectsflask_web ests est_user_model.py",
line 47, in test_valid_confirmation_token
self.assertTrue(u.confirm(token))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 9 tests in 9.055s

FAILED (failures=1)

models.py:

    def confirm(self, token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)#解码令牌
        except:
            return False
        if data.get('confirm') != self.id:
            return False
        self.confirmed =True
        db.session.add(self)
        return True

错误原因:"s.loads(token)"写成了“s.load(token)”

loads()方法:

    def load(self, f, salt=None):
        """Like :meth:`loads` but loads from a file."""
        return self.loads(f.read(), salt)

6. 蓝本

blueprint把实现不同功能的module分开.

Factor an application into a set of blueprints.

Register a blueprint on an application at a URL prefix and/or subdomain.


Register a blueprint multiple times on an application with different URL rules.



原文地址:https://www.cnblogs.com/IDRI/p/6307572.html