欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > flask学习2-应用(博客)

flask学习2-应用(博客)

2025/3/10 23:04:51 来源:https://blog.csdn.net/qq_37954693/article/details/146082892  浏览:    关键词:flask学习2-应用(博客)

flask学习2-应用(博客)

    • 项目目录
    • 应用程序工厂
    • 连接到数据库
    • 建表
    • 初始化数据库文件
    • 蓝图和视图
      • 第一个视图:注册
        • 注册
        • 登录
        • 根据用户id查询用户
        • 注销
      • 模板
        • 基本布局
        • 注册
        • 登录
        • 注册用户
      • 静态文件
      • 博客蓝图
        • 索引
        • 创建
        • 更新-根据id查询
        • 更新-根据id更新
        • 删除
    • 使项目可安装
      • 描述项目
      • 安装项目
    • 测试覆盖率
      • Setup and fixture
    • 部署到生产环境
      • 构建和安装
      • 配置密钥
    • 使用生产服务器运行

项目目录

/home/user/Projects/flask-tutorial
├── flaskr/
│   ├── __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.py
│   ├── test_auth.py
│   └── test_blog.py
├── .venv/
├── pyproject.toml
└── MANIFEST.in
  • .gitignore

    .venv/*.pyc
    __pycache__/instance/.pytest_cache/
    .coverage
    htmlcov/dist/
    build/
    *.egg-info/
    

应用程序工厂

mkdir flaskr
flaskr/__init__.py
import osfrom flask import Flaskdef create_app(test_config=None):# create and configure the appapp = Flask(__name__, instance_relative_config=True)app.config.from_mapping(SECRET_KEY='dev',DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),)if test_config is None:# load the instance config, if it exists, when not testingapp.config.from_pyfile('config.py', silent=True)else:# load the test config if passed inapp.config.from_mapping(test_config)# ensure the instance folder existstry:os.makedirs(app.instance_path)except OSError:pass# a simple page that says hello@app.route('/hello')def hello():return 'Hello, World!'return app
  • 在flask-tutorial中执行flask --app flaskr run --debug

  • http://127.0.0.1:5000/hello

连接到数据库

  • flaskr/db.py

    import sqlite3
    from datetime import datetimeimport click
    from flask import current_app, gdef 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.Rowreturn g.dbdef 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')
    def init_db_command():"""Clear the existing data and create new tables."""init_db()click.echo('Initialized the database.')sqlite3.register_converter("timestamp", lambda v: datetime.fromisoformat(v.decode())
    )# 注册应用程序
    def init_app(app):app.teardown_appcontext(close_db)app.cli.add_command(init_db_command)
    

建表

  • flaskr/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)
    );
    
  • flaskr/init.py

    def create_app():app = ...# existing code omittedfrom . import dbdb.init_app(app)return app
    
  • 初始化数据库文件

    $ flask --app flaskr init-db
    Initialized the database.
    

蓝图和视图

  • Flaskr 将有两个蓝图,一个用于身份验证函数,另一个 一个用于博客文章功能

flaskr/auth.py

import functoolsfrom flask import (Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from werkzeug.security import check_password_hash, generate_password_hashfrom flaskr.db import get_dbbp = Blueprint('auth', __name__, url_prefix='/auth')

flaskr/init.py 注册身份验证

def create_app():app = ...# existing code omittedfrom . import authapp.register_blueprint(auth.bp)return app

第一个视图:注册

flaskr/auth.py

注册
@bp.route('/register', methods=('GET', 'POST'))
def register():if request.method == 'POST':username = request.form['username']password = request.form['password']db = get_db()error = Noneif not username:error = 'Username is required.'elif not password:error = 'Password is required.'if error is None:try:db.execute("INSERT INTO user (username, password) VALUES (?, ?)",(username, generate_password_hash(password)),)db.commit()except db.IntegrityError:error = f"User {username} is already registered."else:return redirect(url_for("auth.login"))flash(error)return render_template('auth/register.html')
登录
 @bp.route('/login', methods=('GET', 'POST'))
def login():if request.method == 'POST':username = request.form['username']password = request.form['password']db = get_db()error = Noneuser = db.execute('SELECT * FROM user WHERE username = ?', (username,)).fetchone()if user is None:error = 'Incorrect username.'elif not check_password_hash(user['password'], password):error = 'Incorrect password.'if error is None:session.clear()session['user_id'] = user['id']return redirect(url_for('index'))flash(error)return render_template('auth/login.html')
根据用户id查询用户
@bp.before_app_request
def load_logged_in_user():user_id = session.get('user_id')if user_id is None:g.user = Noneelse:g.user = get_db().execute('SELECT * FROM user WHERE id = ?', (user_id,)).fetchone()
注销
@bp.route('/logout')
def logout():session.clear()return redirect(url_for('index'))

创建、编辑和删除博客文章将要求用户 已登录。

flaskr/auth.py

# 此装饰器返回一个包装原始视图的新视图函数 它被应用于。新函数检查用户是否已加载,并且 否则重定向到登录页面。如果用户加载了原始 view 被调用并继续正常。在以下情况下,您将使用此装饰器 编写博客视图。
def login_required(view):@functools.wraps(view)def wrapped_view(**kwargs):if g.user is None:return redirect(url_for('auth.login'))return view(**kwargs)return wrapped_view

使用蓝图时,蓝图的名称会附加到 name 的函数

模板

基本布局

flaskr/templates/base.html

<!doctype html>
<title>{% block title %}{% endblock %} - Flaskr</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词