 Flask入门教程数据库工具 (ORM,即对象关系映射)。借助 SQLAlchemy,你可以通过定义 Python 类来表 示数据库里的一张表(类属性表示表中的字段 / 列),通过对这个类进行各种操作 来代替写 SQL 语句。这个类我们称之为模型类,类中的属性我们将称之为字段。 Flask 有大量的第三方扩展,这些扩展可以简化和第三方库的集成工作。我们下面 将使用一个叫做 Flask-SQLAlchemy 的官方扩展来集成 git push 提示 你可以在 GitHub 上查看本书示例程序的对应 commit:4d2442a。 进阶提示 在生产环境,你可以更换更合适的 DBMS,因为 SQLAlchemy 支持多种 SQL 数据库引擎,通常只需要改动非常少的代码。 我们的程序只有一个用户,所以没有将 User 表和 Movie 表建立关联。访问 Flask-SQLAlchemy 文档的”声明模型“章节可以看到相关内容。 query.first() return dict(user=user) # 需要返回字典,等同于return {'user': u ser} 这个函数返回的变量(以字典键值对的形式)将会统一注入到每一个模板的上下文 环境中,因此可以直接在模板中使用。 现在我们可以删除 404 错误处理函数和主页视图函数中的 user 变量定义,并删 除在 render_template() 函数里传入的关键字参数:0 码力 | 127 页 | 7.62 MB | 1 年前3 Flask入门教程数据库工具 (ORM,即对象关系映射)。借助 SQLAlchemy,你可以通过定义 Python 类来表 示数据库里的一张表(类属性表示表中的字段 / 列),通过对这个类进行各种操作 来代替写 SQL 语句。这个类我们称之为模型类,类中的属性我们将称之为字段。 Flask 有大量的第三方扩展,这些扩展可以简化和第三方库的集成工作。我们下面 将使用一个叫做 Flask-SQLAlchemy 的官方扩展来集成 git push 提示 你可以在 GitHub 上查看本书示例程序的对应 commit:4d2442a。 进阶提示 在生产环境,你可以更换更合适的 DBMS,因为 SQLAlchemy 支持多种 SQL 数据库引擎,通常只需要改动非常少的代码。 我们的程序只有一个用户,所以没有将 User 表和 Movie 表建立关联。访问 Flask-SQLAlchemy 文档的”声明模型“章节可以看到相关内容。 query.first() return dict(user=user) # 需要返回字典,等同于return {'user': u ser} 这个函数返回的变量(以字典键值对的形式)将会统一注入到每一个模板的上下文 环境中,因此可以直接在模板中使用。 现在我们可以删除 404 错误处理函数和主页视图函数中的 user 变量定义,并删 除在 render_template() 函数里传入的关键字参数:0 码力 | 127 页 | 7.62 MB | 1 年前3
 Flask Documentation (1.1.x)this: /home/user/Projects/flask-tutorial ├── flaskr/ │ ├── __init__.py │ ├── db.py │ ├── schema.sql │ ├── auth.py │ ├── blog.py │ ├── templates/ │ │ ├── base.html │ │ ├── auth/ │ │ └── update.html │ └── static/ │ └── style.css ├── tests/ │ ├── conftest.py │ ├── data.sql │ ├── test_factory.py │ ├── test_db.py │ ├── test_auth.py │ └── test_blog.py ├── venv/ ├── become big, you may want to switch to a different database. The tutorial doesn’t go into detail about SQL. If you are not familiar with it, the SQLite docs describe the language [https://sqlite.org/lang.html]0 码力 | 428 页 | 895.98 KB | 1 年前3 Flask Documentation (1.1.x)this: /home/user/Projects/flask-tutorial ├── flaskr/ │ ├── __init__.py │ ├── db.py │ ├── schema.sql │ ├── auth.py │ ├── blog.py │ ├── templates/ │ │ ├── base.html │ │ ├── auth/ │ │ └── update.html │ └── static/ │ └── style.css ├── tests/ │ ├── conftest.py │ ├── data.sql │ ├── test_factory.py │ ├── test_db.py │ ├── test_auth.py │ └── test_blog.py ├── venv/ ├── become big, you may want to switch to a different database. The tutorial doesn’t go into detail about SQL. If you are not familiar with it, the SQLite docs describe the language [https://sqlite.org/lang.html]0 码力 | 428 页 | 895.98 KB | 1 年前3
 Flask Documentation (1.1.x)__init__.py db.py schema.sql auth.py blog.py templates/ base.html auth/ login.html register.html blog/ create.html index.html update.html static/ style.css tests/ conftest.py data.sql test_factory.py test_db become big, you may want to switch to a different database. The tutorial doesn’t go into detail about SQL. If you are not familiar with it, the SQLite docs describe the language. Connect to the Database user table, and posts in the post table. Create a file with the SQL commands needed to create empty tables: Listing 5: flaskr/schema.sql DROP TABLE IF EXISTS user; DROP TABLE IF EXISTS post; (continues0 码力 | 291 页 | 1.25 MB | 1 年前3 Flask Documentation (1.1.x)__init__.py db.py schema.sql auth.py blog.py templates/ base.html auth/ login.html register.html blog/ create.html index.html update.html static/ style.css tests/ conftest.py data.sql test_factory.py test_db become big, you may want to switch to a different database. The tutorial doesn’t go into detail about SQL. If you are not familiar with it, the SQLite docs describe the language. Connect to the Database user table, and posts in the post table. Create a file with the SQL commands needed to create empty tables: Listing 5: flaskr/schema.sql DROP TABLE IF EXISTS user; DROP TABLE IF EXISTS post; (continues0 码力 | 291 页 | 1.25 MB | 1 年前3
共 3 条
- 1














