zoukankan      html  css  js  c++  java
  • flask接口入门实现简单的登录注册(一)

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

    首先是初始化app

    __init__.py

    import os
    
    from flask import Flask
    
    
    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 is None:
            # 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)
        except OSError:
            pass
    
        # a simple page that says hello
        @app.route('/hello')
        def hello():
            return 'Hello, World!'
    
        from . import db
        db.init_app(app)
    
    from . import auth
    app.register_blueprint(auth.bp)
    return app

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

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

    import sqlite3
    import click
    from flask import current_app, g
    from flask.cli import with_appcontext
    
    
    def get_db():
        if 'db' not in g:
            g.db = sqlite3.connect(
                current_app.config['DATABASE'],
                detect_types=sqlite3.PARSE_DECLTYPES
            )
            g.db.row_factory = sqlite3.Row
    
        return g.db
    
    
    def close_db(e=None):
        db = g.pop('db', None)
    
        if db is not None:
            db.close()
    
    
    def init_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
    def init_db_command():
        """Clear the existing data and create new tables."""
        init_db()
        click.echo('Initialized the database.')
    
    
    def init_app(app):
        app.teardown_appcontext(close_db)
        app.cli.add_command(init_db_command)

    数据库模型文件

    schema.sql

    DROP TABLE IF EXISTS user;
    DROP TABLE IF EXISTS post;
    
    CREATE TABLE user (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      username TEXT UNIQUE NOT NULL,
      password TEXT NOT NULL
    );
    
    CREATE TABLE post (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      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官方文档来就好了,一般只需要在后面加新的特性就可以了,后面写具体接口事例

  • 相关阅读:
    [Apio2014]回文串
    省队集训day6 C
    省队集训day6 B
    省队集训Day3 light
    省队集训Day3 tree
    多串匹配
    ubuntu卸载福昕阅读器
    greenlet
    python multithread task_done
    mysql参数调优
  • 原文地址:https://www.cnblogs.com/freven/p/11932394.html
Copyright © 2011-2022 走看看