flask 盘

8 篇文章 0 订阅
6 篇文章 0 订阅

在这里插入图片描述

插件
在这里插入图片描述

** db_manager.py 迁移**
在这里插入图片描述

# -*- encoding: utf-8 -*-
"""Todo:"""

from modles import *

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand


manager = Manager(app=app)
migrate = Migrate(db=db, app=app)
manager.add_command("db", MigrateCommand)


if __name__ == '__main__':
    manager.run()

manage.py(程序入口)

# -*- encoding: utf-8 -*-
"""Todo:"""

from apps import app
from modules.web.index import index_blue
from modules.web.goods import goods_blue


app.register_blueprint(index_blue)
app.register_blueprint(goods_blue)



if __name__ == '__main__':
    app.run()


config.py(配置文件)

# -*- encoding: utf-8 -*-
"""Todo:"""
DEBUG = True
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:@localhost:3306/inx?charset=utf8"
SQLALCHEMY_ECHO = False
SECRET_KEY = "ABC"
SQLALCHEMY_TRACK_MODIFICATIONS = False

modles.py

# -*- encoding: utf-8 -*-
"""Todo:"""

from apps import *
from datetime import datetime


class ModelBase():
    create_time = db.Column(db.DateTime, default=datetime.now(), comment="创建时间")
    update_time = db.Column(db.DateTime, default=datetime.now(), onupdate=datetime.now(), comment="修改时间")


class Cate(db.Model, ModelBase):
    id = db.Column(db.Integer, primary_key=True, comment="商品分类主键ID")
    name = db.Column(db.String(100), nullable=False, comment="分类名称")
    goods = db.relationship("Goods", backref="cate")


class Goods(db.Model, ModelBase):
    id = db.Column(db.Integer, primary_key=True, comment="商品主键ID")
    name = db.Column(db.String(100), nullable=False, comment="商品名称")
    sales = db.Column(db.String(255), default="0", comment="商品名称")
    pic = db.Column(db.String(255), comment="商品图片")
    cate_id = db.Column(db.Integer, db.ForeignKey(Cate.id))

创建modules文件夹下创建web文件夹 下创建goods.py 在创建 index.py
index.html

# -*- encoding: utf-8 -*-
"""Todo:"""

from modles import *

index_blue = Blueprint("index", __name__)


@index_blue.route("/")
def index():
    return render_template("web/index.html")

goods.py

# -*- encoding: utf-8 -*-
"""Todo:"""

from modles import *
goods_blue = Blueprint("goods", __name__)


@goods_blue.route("/add_cate", methods=["GET", "POST"])
def add_cate():
    if request.method == "POST":
        name = request.form.get("name")
        try:
            one_cate = Cate(name=name)
            db.session.add(one_cate)
            db.session.commit()
            flash("添加成功")
        except Exception as e:
            flash("添加失败")
    return render_template("web/add_cate.html")


@goods_blue.route("/add_goods", methods=["GET", "POST"])
def add_goods():
    cates = Cate.query.all()
    if request.method == "POST":
        cate_id = request.form.get("cate_id")
        name = request.form.get("name")
        pic = request.files.get("pic")
        if pic:
            pic_url = exam_base1.save(pic)
        else:
            flash("添加图片失败")
        try:
            one_goods = Goods(name=name, cate_id=cate_id, pic=pic_url)
            db.session.add(one_goods)
            db.session.commit()
            flash("添加成功")
        except Exception as e:
            flash("添加失败")
    return render_template("web/add_goods.html", cates=cates)


# @goods_blue.route("/all_cate")
# def all_cate():
#     cates = Cate.query.all()
#     return render_template("web/all_cate.html", cates=cates)

@goods_blue.route("/all_goods")
def all_goods():
    cates = Cate.query.all()
    cate_id = request.args.get("cate_id")
    goods = Goods.query.filter(Goods.cate_id == cate_id).order_by(Goods.sales.desc()).all()
    return render_template("web/all_goods.html", goods=goods, cates=cates)

index.html

{% extends "web/base.html" %}

{% block top %}
    <title>首页</title>
{% endblock top %}

{% block content %}
    {{ super() }}
    <div style="color: green;">
        <h1>首页</h1>
    </div>
{% endblock content %}

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    {% block top %}
        <title>Title</title>
        <script type="text/javascript" src="../../static/web/js/jquery.js"></script>
    {% endblock top %}
    
</head>
<body>
    {% block content %}
        <div style="text-align: center;">
            <a href="{{ url_for("index.index") }}">首页</a>
            <a href="{{ url_for("goods.add_cate") }}">添加分类</a>
            <a href="{{ url_for("goods.add_goods") }}">添加商品</a>
            <a href="{{ url_for("goods.all_goods") }}">所有分类</a>
        </div>
    {% endblock content %}
    {% block goods %}
    {% endblock goods %}
</body>
</html>

add_cate.html

{% extends "web/base.html" %}

{% block top %}
    <title>添加分类</title>
{% endblock top %}

{% block content %}
    {{ super() }}
    <div style="text-align: center;">
        <table align="center">
            <form action="" method="post">
                <tr>
                    <td>商品分类:</td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td colspan="2" align="right"><input type="submit" value="添加"></td>
                </tr>
            </form>
        </table>

        {% for message in get_flashed_messages() %}
            <span style="color:red;">{{ message }}</span>
        {% endfor %}
    </div>
{% endblock content %}

add_goods.html

{% extends "web/base.html" %}

{% block top %}
    <title>添加商品</title>
{% endblock top %}

{% block content %}
    {{ super() }}
    <div style="text-align: center;">
        <table align="center">
            <form action="" method="post" enctype="multipart/form-data">
                <tr>
                    <td>商品分类:</td>

                        <td>

                            <select name="cate_id">
                                {% for cate in cates %}
                                <option value="{{ cate.id }}">{{ cate.name }}</option>
                                {% endfor %}
                            </select>

                        </td>

                </tr>
                <tr>
                    <td>商品名称:</td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td>商品图片:</td>
                    <td><input type="file" name="pic"></td>
                </tr>
                <tr>
                    <td colspan="2" align="right"><input type="submit" value="添加"></td>
                </tr>
            </form>
        </table>

        {% for message in get_flashed_messages() %}
            <span style="color:red;">{{ message }}</span>
        {% endfor %}
    </div>
{% endblock content %}

all_goods.html

{% extends "web/all_cate.html" %}

{% block top %}
    <title></title>
{% endblock top %}

{% block goods %}
    {{ super() }}
    <div>
        <table align="center" cellpadding="2px">
            {% for good in goods %}
            <tr>
                <td><img src="../../static/upload/{{ good.pic }}" alt="图片走丢了" width="100px"></td>
                <td><h1 style="color: #ffbe28">{{ loop.index }}</h1></td>
                <td width="160px">{{ good.name }}</td>
            </tr>
            {% endfor %}
        </table>

    </div>
{% endblock goods %}

all_cate.html

{% extends "web/base.html" %}
{% block goods %}
    <div style="text-align: center;">
        {% for cate in cates %}
            <a href="{{ url_for("goods.all_goods", cate_id=cate.id) }}">{{ cate.name }}</a>|
        {% endfor %}
    </div>
{% endblock goods %}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值