
from flask import Flask, render_template, request, redirect, url_for, flash, session
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('setting')
db = SQLAlchemy(app=app)
''' 创建表 '''
class Books(db.Model):
id = db.Column(db.Integer, primary_key=True, comment='书籍ID')
name = db.Column(db.String(30), unique=True, comment='书籍名称')
price = db.Column(db.DECIMAL, comment='书籍价格')
@app.route('/')
def index():
books = Books.query.order_by(Books.price.desc()).all()
return render_template('index.html', books=books)
@app.route('/add_book', methods=['GET', 'POST'])
def add_book():
if request.method == 'POST':
name = request.form.get('name')
price = request.form.get('price')
new_name = Books(name=name, price=price)
try:
db.session.add(new_name)
db.session.commit()
flash('添加书籍成功!')
except Exception:
flash('添加失败(书籍名称重名或其他)')
return render_template('add_book.html')
@app.route('/alter_book', methods=['GET','POST'])
def alter_book():
book_id = request.args.get('book_id')
one_book = Books.query.get(book_id)
if request.method == 'POST':
new_book = request.form.get('name')
one_book.name = new_book
db.session.add(one_book)
db.session.commit()
return redirect(url_for('index'))
return render_template('alter_book.html', one_book=one_book)
@app.route('/buy_book')
def buy_book():
if session.get('user_id'):
return '可以购买'
else:
return '需要登录'
if __name__ == '__main__':
app.run()
# setting
DEBUG = True # 开启调试模式
SECRET_KEY = "962464" # 随机输入字符串
# SQLALCHEMY_DATABASE_URI = 链接的数据库类型+使用的驱动://用户名:密码@数据库地址:端口/数据库名称?数据库编码
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:mysql@127.0.0.1:3306/exam5?charset=utf8' # 连接数据库
# SQLALCHEMY_ECHO = True # 在控制面板显示
SQLALCHEMY_TRACK_MODIFICATIONS = False
{# base.html #}
{% block top %}
<a href="{{ url_for('index') }}">首页【所有书籍】</a>
<a href="{{ url_for('add_book') }}">添加书籍</a>
{% endblock top %}
{% block content %}
{% endblock content %}
{# index.html #}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>所有书籍</title>
</head>
<body>
{% extends 'base.html' %}
{% block content %}
<table border="1" cellspacing="0">
<tr>
<td>书籍ID</td>
<td>名称</td>
<td>价格</td>
<td>操作</td>
</tr>
{% for book in books %}
<tr>
<td>{{ book.id }}</td>
<td>{{ book.name }}</td>
<td>{{ book.price }}</td>
<td>
<a href="{{ url_for('buy_book', book_id = book.id) }}">购买</a> |
<a href="{{ url_for('alter_book', book_id = book.id) }}">修改书籍:{{ book.name }}</a> |
</td>
</tr>
{% endfor %}
</table>
{% endblock content %}
</body>
</html>
{# add_book.html #}
<!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="">
书籍价格:<input type="text" name="price" value="">
<input type="submit" value="确认添加">
</form>
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}
{% endblock content %}
</body>
</html>
{# alter_book.html #}
<!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="" placeholder="{{ one_book.name }}">
<input type="submit" value="确认修改">
</form>
{% endblock content %}
</body>
</html>