**flask 上传图片和修改图片和分页和搜索
创建modules 文件夹下创建web文件夹index.py
创建static文件夹下创建web文件夹下创建css文件夹创建js文件夹创建tinymce文件夹
创建templates文件夹下创建apps.html。。。。。。。
apps.py
manager.py
models.py
setting.py
start.py
**
插件
start.py
from models import *
from modules.web.index import index_blue
app.register_blueprint(index_blue)
if __name__ == '__main__':
app.run()
**setting.py **
'''配置文件'''
DEBUG = True
SECRET_KEY = 'abc'
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:mysql@127.0.0.1:3306/exam2?charset=utf8'
SQLALCHEMY_ECHO = True
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
PER_PAGE_NUM = 2
from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from apps import app
from models import *
manager = Manager(app=app)
migrate = Migrate(app=app,db=db)
manager.add_command('db',MigrateCommand)
if __name__ == '__main__':
manager.run()
models.py(表)
from apps import *
class Cate(db.Model):
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(50))
class Goods(db.Model):
id = db.Column(db.Integer,primary_key=True)
cname = db.Column(db.String(255))
pic = db.Column(db.Text)
cid = db.Column(db.Integer,db.ForeignKey(Cate.id))
from flask import Flask,Blueprint,render_template,request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('setting')
db = SQLAlchemy(app=app)
创建modules文件夹下创建web文件夹下创建index.py
index.py
from models import *
index_blue = Blueprint('index',__name__)
# 主页
@index_blue.route('/')
def index():
return render_template('web/index.html')
# 添加分类
@index_blue.route('/add_cates',methods=['GET','POST'])
def add_cates():
if request.method == 'POST':
name = request.form.get('name')
new_cate = Cate(name=name)
db.session.add(new_cate)
return render_template('web/add_cate.html')
# 所有分类
@index_blue.route('/all_cates')
def all_cates():
one_cate = Cate.query.all()
return render_template('web/all_cates.html',one_cate=one_cate)
# 添加产品
@index_blue.route('/add_goods',methods=['GET','POST'])
def add_goods():
cid = request.args.get('cid')
if request.method == 'POST':
cname = request.form.get('cname')
pic = request.form.get('pic')
new_goods = Goods(cname=cname,pic=pic,cid=cid)
db.session.add(new_goods)
return render_template('web/add_goods.html')
# 所有产品
@index_blue.route('/all_goods')
def all_goods():
cid = request.args.get('cid')
one_goods = Goods.query.filter(Goods.cid==cid).all()
return render_template('web/all_goods.html',one_goods=one_goods)
# 分页
@index_blue.route('/pages')
def pages():
page = int(request.args.get('page',1))
one_page = Goods.query.paginate(page,2)
return render_template('web/all_goodss.html',one_page=one_page)
# 详情页面
@index_blue.route('/details')
def details():
cid = request.args.get('cid')
one_detail = Goods.query.get(cid)
return render_template('web/details.html',one_detail=one_detail)
创建templates文件夹下创建web文件夹
base.html
{% extends 'web/base.html' %}
{% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>详情页面</title>
</head>
<body>
{{ one_detail.cname }}、{{ one_detail.pic | safe }} <br>
{% endblock content %}
</body>
</html>
index.html
{% extends 'web/base.html' %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
{% block content %}
<h1>首页</h1>
{% endblock content %}
</body>
</html>
add_cate.html
{% extends 'web/base.html' %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加分类</title>
</head>
<body>
{% block content %}
<form action="" method="post">
添加分类:<input type="text" value="" name="name"> <br>
<input type="submit" value="确认添加">
</form>
{% endblock content %}
</body>
</html>
all_cates.html
{% extends 'web/base.html' %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加分类</title>
</head>
<body>
{% block content %}
<form action="" method="post">
添加分类:<input type="text" value="" name="name"> <br>
<input type="submit" value="确认添加">
</form>
{% endblock content %}
</body>
</html>
add_goods.html
{% extends 'web/base.html' %}
{% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加产品</title>
<script src="../../static/web/tinymce/js/tinymce/tinymce.min.js"></script>
<script src="../../static/web/js/tinymce_setup.js"></script>
</head>
<body>
<form action="" method="post">
产品名称:<input type="text" value="" name="cname"> <br>
产品内容:<input type="text" name="pic" value="" id="rich_content">
<input type="submit" value="添加产品">
</form>
{% endblock content %}
</body>
</html>
all_goods.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>所有产品</title>
</head>
<body>
{% block content %}
{% for goods in one_goods %}
{{ goods.id }}、{{ goods.cname }}、{{ goods.pic | safe}} <br>
{% endfor %}
{% endblock content %}
</body>
</html>
all_goodss.html
{% extends 'web/base.html' %}
{% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>所有分类</title>
</head>
<body>
{% for page in one_page.items %}
<a href="{{ url_for('index.details',cid=page.id) }}">{{ page.cname }}</a>、{{ page.pic | safe }}
{% endfor %}
<a href="{{ url_for('index.pages',page=1) }}">1</a>
<a href="{{ url_for('index.pages',page=2) }}">2</a>
{% endblock content %}
</body>
</html>
details.html
{% extends 'web/base.html' %}
{% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>详情页面</title>
</head>
<body>
{{ one_detail.cname }}、{{ one_detail.pic | safe }} <br>
{% endblock content %}
</body>
</html>