flask 图书小项目

创建py插件 setting.py 文件

 DEBUG = True 
 SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:fumingyao@127.0.0.1:3306/test2?charset=utf8'  
SQLALCHEMY_ECHO = True
SQLALCHEMY_TRACK_MODIFICATIONS = False    
 SECRET_KEY = 'abc'

创建start.py文件

from flask import Flask ,render_template,request,url_for,redirect,flash
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config.from_object('setting')

db = SQLAlchemy(app=app)
#作者表
class Author(db.Model):
    id = db.Column(db.Integer,primary_key=True ,comment='作者主键id')
    name = db.Column(db.String(30),nullable=False,comment='作者名字')

    books = db.relationship('Book')

#书籍表
class Book (db.Model):
    id = db.Column(db.Integer,primary_key=True,comment='书籍主建id')
    title = db.Column(db.String(50),nullable=False,comment='书籍名称')
    author_id = db.Column(db.Integer,db.ForeignKey('author.id'),comment='外建指向作者主键id')
#首页
@app.route('/')
def index():
    return render_template('shou_ye.html')

#添加作者
@app.route('/add_author',methods=['GET','POST'])
def add_author():
    if request.method == 'POST':
        name = request.form.get('name')
        new_author = Author(name=name)
        try:
            db.session.add(new_author)
            db.session.commit()
            flash('添加成功')
        except Exception as e:
            flash('添加失败')
    return render_template('add_author.html')

#查询作者
@app.route('/all_authors')
def all_authors():
    authors = Author.query.all()
    return render_template('all_authors.html', authors=authors)

#编辑作者
@app.route('/edit_author',methods=["GET","POST"])
def edit_author():
    author_id = request.args.get('author_id')
    one_author = Author.query.get(author_id)
    if request.method == 'POST':
        name = request.form.get('name')
        one_author.name = name
        try:
            db.session.add(one_author)
            db.session.commit()
            flash('修改成功')
        except Exception as e:
            flash('修改失败')
        return redirect(url_for('all_authors'))
    return render_template('edit_author.html', one_author=one_author)


#删除作者
@app.route('/delete_author')
def delete_author():
    author_id = request.args.get('author_id')
    one_author = Author.query.get(author_id)
    try:
        db.session.delete(one_author)
        db.session.commit()
        flash('删除成功')
    except Exception as e:
        flash('删除失败')
    return redirect(url_for('all_authors'))

#添加书籍
@app.route('/add_book',methods=['GET','POST'])
def add_book():
    author_id = request.args.get('author_id')
    if request.method == 'POST':
        title = request.form.get('title')
        new_book = Book(title=title,author_id=author_id)
        try:
            db.session.add(new_book)
            db.session.commit()
            flash('添加书籍成功')
        except Exception as e:
            flash('添加书籍失败')
        return redirect(url_for('all_authors'))
    return render_template('add_book.html')

#查看书籍
@app.route('/author_books')
def author_books():
    author_id = request.args.get('author_id')
    one_author = Author.query.get(author_id)
    return render_template('author_books.html', one_author=one_author)


#修改书籍
@app.route('/edit_book')
def edit_book():
    book_id=request.args.get('book_id')
    one_book = Book.query.get(book_id)
    if request.method == 'POST':
        title = request.form.get('title')
        one_book.titlt = title
        db.session.add=(one_book)
        db.session.commit()
        return redirect(url_for('author_books',author_id=one_book.author_id))
    return render_template('edit_book.html', one_book=one_book)


#删除书籍
@app.route('/delete_book')
def delete_book():
    book_id = request.args.get('book_id')
    one_book = Book.query.get(book_id)
    db.session.delete(one_book)
    db.session.commit()
    return redirect(url_for('author_books',author_id=one_book.author_id))

@app.route('/delete_books',methods=['POST'])
def delete_books():
    book_ids= request.form.getlist('book_ids')
    for book_id in book_ids:
        # print(book_ids)
        # print(type(book_id))
        one_book = Book.query.get(int(book_id))
        db.session.delete(one_book)
        db.session.commit()
    return redirect(url_for('author_books',author_id=one_book.author_id))

if __name__ == '__main__':
    # db.drop_all()
    # db.create_all()
    app.run(port=9852)

创建templates文件夹
在该文件里创建HTML

添加作者html:add_author

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加作者</title>
</head>
<body>
    {% extends 'base.html' %}
        {% block content %}
            <form action="" method="post">
                作者名称:<input type="text" name="name" value=""> <br>
                <input type="submit" value="确定">
            </form>
            {% for message in get_flashed_messages() %}
                <span style="color: darkorchid">{{ message }}</span>
            {% endfor %}
        {% endblock content%}

</body>
</html>

添加书籍html:add_book

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加书籍</title>
</head>
<body>
    {% extends 'base.html' %}
    {% block content %}
        <form action="" method="post">
            书籍名称:<input type="text" name="title" value=""> <br>
            <input type="submit" value="确定">
        </form>
        {% for message in get_flashed_messages()  %}
            <span style="color: darkorchid">{{ message }}</span>
        {% endfor %}
    {% endblock content %}

</body>
</html>

    查看作者html:all_authors
 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查看作者</title>
</head>
<body>
    {% extends 'base.html' %}
    {% block content %}
        {% for author in authors %}
            <p>
                {{ author.id }} {{ author.name }}
                <a href="{{ url_for('edit_author',author_id=author.id) }}">编辑</a>
                <a href="{{ url_for('delete_author',author_id=author.id) }}">删除</a>
                <a href="{{ url_for('add_book',author_id=author.id) }}">添加书籍</a>
                <a href="{{ url_for('author_books',author_id=author.id) }}">查看{{ author.name }}的书籍</a>
            </p>
        {% endfor %}
        <p>
        {% for message in get_flashed_messages() %}
            <span style="color: chartreuse">{{ message }}</span>
        {% endfor %}
        </p>
    {% endblock content %}
</body>
</html>

        查看作者书籍html:suthor_books

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>作者书籍</title>
</head>
<body>
    {% extends 'base.html' %}
    {% block content %}
        <form action="{{ url_for('delete_books') }}" method="post">
            {% for book in one_author.books %}
                <p>
                    <input type="checkbox" name="book_ids" value="{{ book.id }}">
                    {{ loop.index }} 、{{ book.title }}|
                    <a href="{{ url_for('edit_book',book_id=book.id) }}">编辑{{ one_author.name }}书籍</a>
                    <a href="{{ url_for('delete_book',book_id=book.id) }}">删除{{ one_author.name }}书籍</a>
                </p>
            {% endfor %}
            <input type="submit" value="批量删除">
        </form>
    {% endblock content%}

</body>
</html>


创建继承html:base
{% block top %}
    <a href="{{ url_for('index') }}"> 首页 </a>
    <a href="{{ url_for('add_author') }}"> 添加作者 </a>
    <a href="{{ url_for('all_authors') }}"> 查看作者 </a>
{% endblock top %}
    <hr>
{% block content %}

{% endblock content %}



 编辑作者html:edit_author
 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑作者</title>
</head>
<body>
    {% extends 'base.html' %}
        {% block content %}
            <form action="" method="post">
                作者名称:<input type="text" name="name" value="{{ one_author.name }}">  <br>
                <input type="submit" value="确定">
            </form>
            {% for message in get_flashed_messages() %}
                <span style="color: chartreuse">{{ message }}</span>
            {% endfor %}
        {% endblock content %}
</body>
</html>



 创建继承html:edit_book
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑书籍</title>
</head>
<body>
{% extends 'base.html' %}
    {% block content %}
        <form action="" method="post">
            书籍名称:<input type="text" name="title" value="{{ one_book.title }}">
            <input type="submit" value="确定修改">
        </form>
    {% endblock content %}
</body>
</html>


 创建首页html:shou_ye
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    {% extends 'base.html' %}
    {% block content %}
        <h1>fumingayo</h1>
    {% endblock content %}
</body>
</html>

#请见谅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值