flask学习2-应用(博客)

打印 上一主题 下一主题

主题 1870|帖子 1870|积分 5610

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
项目目次

  1. /home/user/Projects/flask-tutorial
  2. ├── flaskr/
  3. │   ├── __init__.py
  4. │   ├── db.py
  5. │   ├── schema.sql
  6. │   ├── auth.py
  7. │   ├── blog.py
  8. │   ├── templates/
  9. │   │   ├── base.html
  10. │   │   ├── auth/
  11. │   │   │   ├── login.html
  12. │   │   │   └── register.html
  13. │   │   └── blog/
  14. │   │       ├── create.html
  15. │   │       ├── index.html
  16. │   │       └── update.html
  17. │   └── static/
  18. │       └── style.css
  19. ├── tests/
  20. │   ├── conftest.py
  21. │   ├── data.sql
  22. │   ├── test_factory.py
  23. │   ├── test_db.py
  24. │   ├── test_auth.py
  25. │   └── test_blog.py
  26. ├── .venv/
  27. ├── pyproject.toml
  28. └── MANIFEST.in
复制代码


  • .gitignore
    1. .venv/
    2. *.pyc
    3. __pycache__/
    4. instance/
    5. .pytest_cache/
    6. .coverage
    7. htmlcov/
    8. dist/
    9. build/
    10. *.egg-info/
    复制代码
应用程序工厂

  1. mkdir flaskr
  2. flaskr/__init__.py
复制代码
  1. import os
  2. from flask import Flask
  3. def create_app(test_config=None):
  4.     # create and configure the app
  5.     app = Flask(__name__, instance_relative_config=True)
  6.     app.config.from_mapping(
  7.         SECRET_KEY='dev',
  8.         DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
  9.     )
  10.     if test_config is None:
  11.         # load the instance config, if it exists, when not testing
  12.         app.config.from_pyfile('config.py', silent=True)
  13.     else:
  14.         # load the test config if passed in
  15.         app.config.from_mapping(test_config)
  16.     # ensure the instance folder exists
  17.     try:
  18.         os.makedirs(app.instance_path)
  19.     except OSError:
  20.         pass
  21.     # a simple page that says hello
  22.     @app.route('/hello')
  23.     def hello():
  24.         return 'Hello, World!'
  25.     return app
复制代码


  • 在flask-tutorial中实验flask --app flaskr run --debug

  • http://127.0.0.1:5000/hello
毗连到数据库



  • flaskr/db.py
    1. import sqlite3
    2. from datetime import datetime
    3. import click
    4. from flask import current_app, g
    5. def get_db():
    6.     if 'db' not in g:
    7.         g.db = sqlite3.connect(
    8.             current_app.config['DATABASE'],
    9.             detect_types=sqlite3.PARSE_DECLTYPES
    10.         )
    11.         g.db.row_factory = sqlite3.Row
    12.     return g.db
    13. def close_db(e=None):
    14.     db = g.pop('db', None)
    15.     if db is not None:
    16.         db.close()
    17.         
    18. def init_db():
    19.     db = get_db()
    20.     with current_app.open_resource('schema.sql') as f:
    21.         db.executescript(f.read().decode('utf8'))
    22. @click.command('init-db')
    23. def init_db_command():
    24.     """Clear the existing data and create new tables."""
    25.     init_db()
    26.     click.echo('Initialized the database.')
    27. sqlite3.register_converter(
    28.     "timestamp", lambda v: datetime.fromisoformat(v.decode())
    29. )
    30. # 注册应用程序
    31. def init_app(app):
    32.     app.teardown_appcontext(close_db)
    33.     app.cli.add_command(init_db_command)
    复制代码
建表



  • flaskr/schema.sql
    1. DROP TABLE IF EXISTS user;
    2. DROP TABLE IF EXISTS post;
    3. CREATE TABLE user (
    4.   id INTEGER PRIMARY KEY AUTOINCREMENT,
    5.   username TEXT UNIQUE NOT NULL,
    6.   password TEXT NOT NULL
    7. );
    8. CREATE TABLE post (
    9.   id INTEGER PRIMARY KEY AUTOINCREMENT,
    10.   author_id INTEGER NOT NULL,
    11.   created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    12.   title TEXT NOT NULL,
    13.   body TEXT NOT NULL,
    14.   FOREIGN KEY (author_id) REFERENCES user (id)
    15. );
    复制代码

  • flaskr/init.py
    1. def create_app():
    2.     app = ...
    3.     # existing code omitted
    4.     from . import db
    5.     db.init_app(app)
    6.     return app
    复制代码

  • 初始化数据库文件

    1. $ flask --app flaskr init-db
    2. Initialized the database.
    复制代码
蓝图和视图



  • Flaskr 将有两个蓝图,一个用于身份验证函数,另一个 一个用于博客文章功能
flaskr/auth.py
  1. import functools
  2. from flask import (
  3.     Blueprint, flash, g, redirect, render_template, request, session, url_for
  4. )
  5. from werkzeug.security import check_password_hash, generate_password_hash
  6. from flaskr.db import get_db
  7. bp = Blueprint('auth', __name__, url_prefix='/auth')
复制代码
flaskr/init.py 注册身份验证
  1. def create_app():
  2.     app = ...
  3.     # existing code omitted
  4.     from . import auth
  5.     app.register_blueprint(auth.bp)
  6.     return app
复制代码
第一个视图:注册

flaskr/auth.py
注册

  1. @bp.route('/register', methods=('GET', 'POST'))
  2. def register():
  3.     if request.method == 'POST':
  4.         username = request.form['username']
  5.         password = request.form['password']
  6.         db = get_db()
  7.         error = None
  8.         if not username:
  9.             error = 'Username is required.'
  10.         elif not password:
  11.             error = 'Password is required.'
  12.         if error is None:
  13.             try:
  14.                 db.execute(
  15.                     "INSERT INTO user (username, password) VALUES (?, ?)",
  16.                     (username, generate_password_hash(password)),
  17.                 )
  18.                 db.commit()
  19.             except db.IntegrityError:
  20.                 error = f"User {username} is already registered."
  21.             else:
  22.                 return redirect(url_for("auth.login"))
  23.         flash(error)
  24.     return render_template('auth/register.html')
复制代码
登录

  1. @bp.route('/login', methods=('GET', 'POST'))
  2. def login():
  3.     if request.method == 'POST':
  4.         username = request.form['username']
  5.         password = request.form['password']
  6.         db = get_db()
  7.         error = None
  8.         user = db.execute(
  9.             'SELECT * FROM user WHERE username = ?', (username,)
  10.         ).fetchone()
  11.         if user is None:
  12.             error = 'Incorrect username.'
  13.         elif not check_password_hash(user['password'], password):
  14.             error = 'Incorrect password.'
  15.         if error is None:
  16.             session.clear()
  17.             session['user_id'] = user['id']
  18.             return redirect(url_for('index'))
  19.         flash(error)
  20.     return render_template('auth/login.html')
复制代码
根据用户id查询用户

  1. @bp.before_app_request
  2. def load_logged_in_user():
  3.     user_id = session.get('user_id')
  4.     if user_id is None:
  5.         g.user = None
  6.     else:
  7.         g.user = get_db().execute(
  8.             'SELECT * FROM user WHERE id = ?', (user_id,)
  9.         ).fetchone()
复制代码
注销

  1. @bp.route('/logout')
  2. def logout():
  3.     session.clear()
  4.     return redirect(url_for('index'))
复制代码
创建、编辑和删除博客文章将要求用户 已登录。
flaskr/auth.py
  1. # 此装饰器返回一个包装原始视图的新视图函数 它被应用于。新函数检查用户是否已加载,并且 否则重定向到登录页面。如果用户加载了原始 view 被调用并继续正常。在以下情况下,您将使用此装饰器 编写博客视图。
  2. def login_required(view):
  3.     @functools.wraps(view)
  4.     def wrapped_view(**kwargs):
  5.         if g.user is None:
  6.             return redirect(url_for('auth.login'))
  7.         return view(**kwargs)
  8.     return wrapped_view
复制代码
使用蓝图时,蓝图的名称会附加到 name 的函数
模板

根本布局

flaskr/templates/base.html
  1. <!doctype html>
  2. <title>{% block title %}{% endblock %} - Flaskr</title>
  3. <link rel="stylesheet" href="{
  4.     { url_for('static', filename='style.css') }}">
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

石小疯

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表