flask接口入门实现简单的登录注册(一)

摘要:
和网上的博客app不同,由于需求主要写一些小程序的借口首先是初始化app__init__.pyimportosfromflaskimportFlaskdefcreate_app(test_config=None):#createandconfiguretheappapp=Flask(__name__,instance_relative_config=True)app.config.from_map

和网上的博客app不同,由于需求主要写一些小程序的借口

首先是初始化app

__init__.py

importos

from flask importFlask


def create_app(test_config=None):
    #create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'app.sqlite'),
    )

    if test_config isNone:
        #load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        #load the test config if passed in
app.config.from_mapping(test_config)

    #ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    exceptOSError:
        pass

    #a simple page that says hello
    @app.route('/hello')
    defhello():
        return 'Hello, World!'

    from . importdb
    db.init_app(app)

from . import auth
app.register_blueprint(auth.bp)
return app

这一部分完成就可以运行,通过访问127.0.0.1/5000/hello来检测应用是否创建成功

db.py '''数据库操作初始化'''

importsqlite3
importclick
from flask importcurrent_app, g
from flask.cli importwith_appcontext


defget_db():
    if 'db' not ing:
        g.db =sqlite3.connect(
            current_app.config['DATABASE'],
            detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory =sqlite3.Row

    returng.db


def close_db(e=None):
    db = g.pop('db', None)

    if db is notNone:
        db.close()


definit_db():
    db =get_db()

    with current_app.open_resource('schema.sql') as f:
        db.executescript(f.read().decode('utf8'))


@click.command('init-db')
@with_appcontext
definit_db_command():
    """Clear the existing data and create new tables."""init_db()
    click.echo('Initialized the database.')


definit_app(app):
    app.teardown_appcontext(close_db)
    app.cli.add_command(init_db_command)

数据库模型文件

schema.sql

DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTSpost;

CREATE TABLE user(
  id INTEGER PRIMARY KEYAUTOINCREMENT,
  username TEXT UNIQUE NOT NULL,
  password TEXT NOT NULL);

CREATE TABLEpost (
  id INTEGER PRIMARY KEYAUTOINCREMENT,
  author_id INTEGER NOT NULL,
  created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  title TEXT NOT NULL,
  body TEXT NOT NULL,
  FOREIGN KEY (author_id) REFERENCES user(id)
);

这一部分按照flask官方文档来就好了,一般只需要在后面加新的特性就可以了,后面写具体接口事例

免责声明:文章转载自《flask接口入门实现简单的登录注册(一)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android抓包方法(转)redis慢查询的简单认识和事务、订阅的认识下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

应用性能监控(二)

Zipkin 是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper 的论文设计而来,由 Twitter公司开发贡献。其主要功能是聚集来自各个异构系统的实时监控数据,用来追踪微服务架构下的系统延时问题。应用系统需要进行装备(instrument)以向 Zipkin 报告数据。Zipki...

【原生】CocosCreator apk打包发布 (APPABI、jsc、网易易盾、权限、app名称图标、包名、签名)

Cocos版本:2.4.4  一 打包apk 二 App ABi的勾选 三 加密脚本 四 网易易盾插件 五 其它问题 一 打包apk 在android studio中,选择生成带签名的apk  选择APK 选择Module处,选择你的项目。 然后如果已有签名jks文件则选择choose existing,并输入你的key store密码。  没有jks...

flask 使用Flask-SQLAlchemy管理数据库(连接数据库服务器、定义数据库模型、创建库和表) --

使用Flask-SQLAlchemy管理数据库 扩展Flask-SQLAlchemy集成了SQLAlchemy,它简化了连接数据库服务器、管理数据库操作会话等各种工作,让Flask中的数据处理体验变得更轻松。首先使用pipenv安装Flask-SQLAlchemy以及其依赖(主要是SQLAlchemy): pipenv install flask-sqla...

用HTML,CSS和JavaScript创建iPhone/iPad应用程序

象大多数iPad粉丝和程序员一样,我有一个梦想,那就是如果不需要昂贵的Apple设备,不要繁琐的审批程序,不要为发布应用交钱就能开发iPad 应用程序该多好.现在这个梦想就要实现了,那就是网页应用程序. 那么,怎样才能让网页象看起来象本机程序一样呢?你需要做这些工作: 全屏幕:(去掉浏览器的地址栏和按钮栏);防止窗口滚动和缩放; 响应多点触摸和手势事件;...

git基础

1. Git配置 Git 提供了一个叫做git config 的工具,专门用来配置或读取相应的工作环境变量。这些环境变量,可以存放在以下三个不同的地方: /etc/gitconfig文件:系统中对所有用户都普遍适用的配置。若使用git config 时用--system 选项,读写的就是这个文件。 ~/.gitconfig文件:用户目录下的配置文件只适...

PHP-数据库操作

连接数据库 PHP操作数据库的方式有多种如 mysql 、mysqli、PDO,目前主要使用的是PDO处理。 PDO 提供了一个数据访问抽象层,这意味着,不管使用哪种数据库,都可以用相同的函数(方法)来查询和获取数据。 下面是使用 PDO 连接数据库的操作,当连接失败时将抛出异常。 <?php header('Content-type:text/ht...