Flask入门教程22 第 3 章:模板 在一般的 Web 程序里,访问一个地址通常会返回一个包含各类信息的 HTML 页 面。因为我们的程序是动态的,页面中的某些信息需要根据不同的情况来进行调 整,比如对登录和未登录用户显示不同的信息,所以页面需要在用户访问时根据程 序逻辑动态生成。 我们把包含变量和运算逻辑的 HTML 或其他格式的文本叫做模板,执行这些变量替 换和逻辑计算工作的过程被称为渲染,这个工作由我们这一章要学习使用的模板渲 开发实战》的读者,模板相关内容可以在第 3 章《模 板》找到,Faker 相关内容可以在第 7 章找到。 第 3 章:模板 28 第 4 章:静态文件 静态文件(static files)和我们的模板概念相反,指的是内容不需要动态生成的文 件。比如图片、CSS 文件和 JavaScript 脚本等。 在 Flask 中,我们需要创建一个 static 文件夹来保存静态文件,它应该和程序模 块、templates 文件夹 数据库工具 (ORM,即对象关系映射)。借助 SQLAlchemy,你可以通过定义 Python 类来表 示数据库里的一张表(类属性表示表中的字段 / 列),通过对这个类进行各种操作 来代替写 SQL 语句。这个类我们称之为模型类,类中的属性我们将称之为字段。 Flask 有大量的第三方扩展,这些扩展可以简化和第三方库的集成工作。我们下面 将使用一个叫做 Flask-SQLAlchemy 的官方扩展来集成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)__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













