Flask入门教程设置数据库 URI 第 5 章:数据库 35 为了设置 Flask、扩展或是我们程序本身的一些行为,我们需要设置和定义一些配 置变量。Flask 提供了一个统一的接口来写入和获取这些配置变 量: Flask.config 字典。配置变量的名称必须使用大写,写入配置的语句一般 会放到扩展类实例化语句之前。 下面写入了一个 SQLALCHEMY_DATABASE_URI 变量来告诉 接地址: import os # ... app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////' + os.path. join(app.root_path, 'data.db') 注意 这个配置变量的最后一个单词是 URI,而不是 URL。 对于这个变量值,不同的 DBMS 有不同的格式,对于 SQLite 来说,这个值的格式 如下: 称和后缀你可以自由定义,一般会使用 .db、.sqlite 和 .sqlite3 作为后缀。 另外,如果你使用 Windows 系统,上面的 URI 前缀部分需要写入三个斜线(即 sqlite:/// )。在本书的示例程序代码里,做了一些兼容性处理,另外还新设 置了一个配置变量,实际的代码如下: 第 5 章:数据库 36 import os import sys from flask import0 码力 | 127 页 | 7.62 MB | 1 年前3
Flask-RESTful Documentation Release 0.3.10from 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 年前3
Flask-RESTful Documentation
Release 0.3.10response. 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 年前3
Flask-RESTful Documentation
Release 0.3.6OrderedDict 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 年前3
Flask-RESTful Documentation Release 0.3.6from 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 年前3
Flask-RESTful Documentation
Release 0.3.7response. 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 年前3
Flask-RESTful Documentation
Release 0.3.8response. 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 年前3
Flask-RESTful Documentation Release 0.3.8from 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
Flask-RESTful Documentation Release 0.3.7from 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.21 KB | 1 年前3
Flask Documentation (1.1.x)configuration: class Config(object): DEBUG = False TESTING = False DATABASE_URI = 'sqlite:///:memory:' class ProductionConfig(Config): DATABASE_URI = 'mysql://user@localhost/foo' (continues on next page) 80 Chapter database server.""" DEBUG = False TESTING = False DB_SERVER = '192.168.1.56' @property def DATABASE_URI(self): # Note: all caps return 'mysql://user@{}/foo'.format(self.DB_SERVER) class ProductionConfig(Config): 'localhost' DEBUG = True class TestingConfig(Config): DB_SERVER = 'localhost' DEBUG = True DATABASE_URI = 'sqlite:///:memory:' There are many different ways and it’s up to you how you want to manage your0 码力 | 291 页 | 1.25 MB | 1 年前3
共 11 条
- 1
- 2













