为了账号安全,请及时绑定邮箱和手机立即绑定

Flask 好玩的小案例

标签:
Python

Flask
程序目录结构很简单,就一个templates 文件夹,下面有个index.html 文件(代码在下面);还有个app.py 文件(代码在下面)
需要连接mysql 数据库创建一个flask_book 的数据库,特别需要注意的是创建数据库时,指明数据库编码为utf8
python3 flask mysql
增加、删除、查询小案例
要实现上面的案例需要安装哪些依赖包

pip install flask
pip install flask-sqlalchemy
pip install pymysql
pip install flask-wtf

1、python 连接数据库
2、在python 给数据库中创建表、设置主键、外键、设置表与表之间的关系
3、往表中添加数据
4、查询数据,将查询的数据在浏览器中显示数据
5、Form 表单
6、接受form 表单中传入的参数
7、重定向
8、闪现,显示提示信息

from flask import Flask, render_template, flash, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired

app = Flask(__name__)

# CREATE DATABASE flask_books CHARACTER SET utf8

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:@localhost:3306/flask_books"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

app.secret_key = 'Curtis&|'

db = SQLAlchemy(app)

class Author(db.Model):
    __tablename__ = 'authors'

    id = db.Column(db.Integer, primary_key=True)
    author_name = db.Column(db.String(16), unique=True)
    books = db.relationship('Book', backref='author')

    def __repr__(self):
        return 'Author:%s' % self.name

class Book(db.Model):
    __tablename__ = 'books'
    id = db.Column(db.Integer, primary_key=True)
    book_name = db.Column(db.String(16), unique=True)
    author_id = db.Column(db.Integer, db.ForeignKey('authors.id'))

    def __repr__(self):
        return 'Books:%s %s' % (self.name, self.author_id)

# 自定义表单类
class AuthorForm(FlaskForm):
    author = StringField('作者:', validators=[DataRequired()])
    book = StringField('书籍:', validators=[DataRequired()])
    submit = SubmitField('提交')

@app.route('/', methods=['GET', 'POST'])
def index():
    author_form = AuthorForm()

    if author_form.validate_on_submit():
        author_name = author_form.author.data
        book_name = author_form.book.data
        author = Author.query.filter_by(author_name=author_name).first()
        if author:
            book = Book.query.filter_by(book_name=book_name).first()
            if book:
                flash("已存在同名书籍")
            else:
                try:
                    new_book = Book(book_name=book_name, author_id=author.id)
                    db.session.add(new_book)
                    db.session.commit()
                except Exception as e:
                    print(e)
                    flash("添加书籍失败")
                    db.session.rollback()
        else:
            new_author = Author(author_name=author_name)
            db.session.add(new_author)
            db.session.commit()
            new_book = Book(book_name=book_name, author_id=new_author.id)
            db.session.add(new_book)
            db.session.commit()
    else:
        if request.method == 'POST':
            flash("参数不全")

    authors = Author.query.all()

    return render_template('book.html', authors=authors, form=author_form)

@app.route('/delete_book/<book_id>')
def delete_book(book_id):
    book = Book.query.get(book_id)
    if book:
        try:
            db.session.delete(book)
            db.session.commit()
        except Exception as e:
            print(e)
            flash("删除书籍出错")
            db.session.rollback()
    else:
        flash("未找到要删除的数据")
    return redirect(url_for('index'))

@app.route('/delete_author/<author_id>')
def delete_author(author_id):
    author = Author.query.get(author_id)
    if author:
        try:
            Book.query.filter_by(author_id=author_id).delete()
            db.session.delete(author)
            db.session.commit()
        except Exception as e:
            print(e)
            flash("删除作者失败")
            db.session.rollback()
    else:
        flash("作者未找到")
    return redirect(url_for('index'))

if __name__ == "__main__":
    # db.drop_all()
    # db.create_all()
    #
    # au1 = Author(author_name='尤瓦尔·赫拉利')
    # au2 = Author(author_name='瑞·达利欧')
    # au3 = Author(author_name='稻盛和夫')
    #
    # db.session.add_all([au1, au2, au3])
    # db.session.commit()
    #
    # bk1 = Book(book_name='未来简史', author_id=au1.id)
    # bk2 = Book(book_name='原则', author_id=au2.id)
    # bk3 = Book(book_name='活法', author_id=au3.id)
    # bk4 = Book(book_name='干法', author_id=au3.id)
    #
    # db.session.add_all([bk1, bk2, bk3, bk4])
    # db.session.commit()

    app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Book</title>
</head>
<body>
<form method="post">
    {{form.csrf_token()}}<br>
    {{form.author.label}}{{form.author}}<br>
    {{form.book.label}}{{form.book}}<br>
    {{form.submit}}

    {%for message in get_flashed_messages()%}
    {{message}}
    {%endfor%}
</form>
<hr>
<ul>
    {% for author in authors %}
    <li>{{ author.author_name }} <a href="{{ url_for('delete_author',author_id=author.id) }}">删除</a></li>
    <ul>
        {% for book in author.books %}
        <li>
            {{ book.book_name }} <a href="{{ url_for('delete_book',book_id=book.id) }}">删除</a>
        </li>
        {% else %}
        <li>无</li>
        {% endfor %}
    </ul>
    {% endfor %}
</ul>
</body>
</html>
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消