Flask入门教程
用的模板渲 染引擎——Jinja2 来完成。 按照默认的设置,Flask 会从程序实例所在模块同级目录的 templates 文件夹中寻 找模板,我们的程序目前存储在项目根目录的 app.py 文件里,所以我们要在项目 根目录创建这个文件夹: $ mkdir templates 模板基本语法 在社交网站上,每个人都有一个主页,借助 Jinja2 就可以写出一个通用的模板:{{ 第 3 章:模板 23 模板中使用的变量需要在渲染的时候传递进去,具体我们后面会了解。 编写主页模板 我们先在 templates 目录下创建一个 index.html 文件,作为主页模板。主页需要显 示电影条目列表和个人信息,代码如下所示: templates/index.html:主页模板 过滤器来获取 movies 的长度,类似 Python 里的 len() 函数。 第 3 章:模板 24 提示 访问 http://jinja.pocoo.org/docs/2.10/templates/#list-of-builtin-filters 查看所有 可用的过滤器。 准备虚拟数据 为了模拟页面渲染,我们需要先创建一些虚拟数据,用来填充页面内容: app.py:定义虚拟数据
0 码力 | 127 页 | 7.62 MB | 1 年前3Flask Documentation (1.1.x)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.6 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . when getting started. By conven- tion, templates and static files are stored in subdirectories within the application’s Python source tree, with the names templates and static respectively. While this can your application serves. • MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks. • ItsDangerous securely signs data to ensure its integrity. This is0 码力 | 291 页 | 1.25 MB | 1 年前3Flask Documentation (1.1.x)
Minimal Application What to do if the Server does not Start Debug Mode Routing Static Files Rendering Templates Accessing Request Data Redirects and Errors About Responses Sessions Message Flashing Logging Hooking Access the Database Blueprints and Views Templates Static Files Blog Blueprint Make the Project Installable Test Coverage Deploy to Production Keep Developing! Templates Jinja Setup Standard Context Standard when getting started. By convention, templates and static files are stored in subdirectories within the application’s Python source tree, with the names templates and static respectively. While this can0 码力 | 428 页 | 895.98 KB | 1 年前3Flask-RESTful Documentation Release 0.3.10
from flask_restful import fields, marshal_with resource_fields = { 'task': fields.String, 'uri': fields.Url('todo_ep') } class TodoDao(object): def __init__(self, todo_id, task): Url & Other Concrete Fields Flask-RESTful includes a special field, fields.Url, that synthesizes a uri for the resource that’s being requested. This is also a good example of how to add data to your response called api.add_resource() 'uri': fields.Url('todo_resource'), 'random': RandomNumber, } By default fields.Url returns a relative uri. To generate an absolute uri that includes the scheme, hostname0 码力 | 42 页 | 84.60 KB | 1 年前3Flask-RESTful Documentation Release 0.3.10
response. from flask_restful import fields, marshal_with resource_fields = { 'task': fields.String, 'uri': fields.Url('todo_ep') } class TodoDao(object): def __init__(self, todo_id, task): self.todo_id Url & Other Concrete Fields Flask-RESTful includes a special field, fields.Url, that synthesizes a uri for the resource that’s being requested. This is also a good example of how to add data to your response when you called api.add_resource() 'uri': fields.Url('todo_resource'), 'random': RandomNumber, } By default fields.Url returns a relative uri. To generate an absolute uri that includes the scheme, hostname0 码力 | 39 页 | 212.29 KB | 1 年前3Flask-RESTful Documentation Release 0.3.6
OrderedDict from flask_restful import fields, marshal_with resource_fields = { 'task': fields.String, 'uri': fields.Url('todo_ep') } 6 Chapter 1. User’s Guide Flask-RESTful Documentation, Release 0.3.6 class Url & Other Concrete Fields Flask-RESTful includes a special field, fields.Url, that synthesizes a uri for the resource that’s being requested. This is also a good example of how to add data to your response add_resource() 'uri': fields.Url('todo_resource'), 1.4. Output Fields 15 Flask-RESTful Documentation, Release 0.3.6 'random': RandomNumber, } By default fields.Url returns a relative uri. To generate0 码力 | 46 页 | 245.60 KB | 1 年前3Flask-RESTful Documentation Release 0.3.6
from flask_restful import fields, marshal_with resource_fields = { 'task': fields.String, 'uri': fields.Url('todo_ep') } class TodoDao(object): def __init__(self, todo_id, task): Url & Other Concrete Fields Flask-RESTful includes a special field, fields.Url, that synthesizes a uri for the resource that’s being requested. This is also a good example of how to add data to your response called api.add_resource() 'uri': fields.Url('todo_resource'), 'random': RandomNumber, } By default fields.Url returns a relative uri. To generate an absolute uri that includes the scheme, hostname0 码力 | 49 页 | 91.90 KB | 1 年前3Flask-RESTful Documentation Release 0.3.7
response. from flask_restful import fields, marshal_with resource_fields = { 'task': fields.String, 'uri': fields.Url('todo_ep') } class TodoDao(object): def __init__(self, todo_id, task): self.todo_id Url & Other Concrete Fields Flask-RESTful includes a special field, fields.Url, that synthesizes a uri for the resource that’s being requested. This is also a good example of how to add data to your response when you called api.add_resource() 'uri': fields.Url('todo_resource'), 'random': RandomNumber, } By default fields.Url returns a relative uri. To generate an absolute uri that includes the scheme, hostname0 码力 | 50 页 | 253.09 KB | 1 年前3Flask-RESTful Documentation Release 0.3.8
response. from flask_restful import fields, marshal_with resource_fields = { 'task': fields.String, 'uri': fields.Url('todo_ep') } class TodoDao(object): def __init__(self, todo_id, task): self.todo_id Url & Other Concrete Fields Flask-RESTful includes a special field, fields.Url, that synthesizes a uri for the resource that’s being requested. This is also a good example of how to add data to your response when you called api.add_resource() 'uri': fields.Url('todo_resource'), 'random': RandomNumber, } By default fields.Url returns a relative uri. To generate an absolute uri that includes the scheme, hostname0 码力 | 50 页 | 253.64 KB | 1 年前3Flask-RESTful Documentation Release 0.3.8
from flask_restful import fields, marshal_with resource_fields = { 'task': fields.String, 'uri': fields.Url('todo_ep') } class TodoDao(object): def __init__(self, todo_id, task): Url & Other Concrete Fields Flask-RESTful includes a special field, fields.Url, that synthesizes a uri for the resource that’s being requested. This is also a good example of how to add data to your response called api.add_resource() 'uri': fields.Url('todo_resource'), 'random': RandomNumber, } By default fields.Url returns a relative uri. To generate an absolute uri that includes the scheme, hostname0 码力 | 55 页 | 93.30 KB | 1 年前3
共 11 条
- 1
- 2