1. 创建订单表
| |
| |
| from django.db import models |
| |
| class Order(models.Model): |
| order_id = models.CharField(verbose_name="订单号", max_length=64) |
| commodity_id = models.CharField(verbose_name="商品", max_length=32) |
| num = models.IntegerField(verbose_name="数量") |
| account = models.DecimalField(verbose_name="价格", max_digits=10, decimal_places=2) |
| create_time = models.DateTimeField(verbose_name="下单时间") |
| depart = models.ForeignKey(verbose_name="配送类型", to="Home", to_field="id", on_delete=models.CASCADE) |
| |
| attribute_choices = ( |
| (1, "自营"), |
| (2, "联营") |
| ) |
| attribute = models.SmallIntegerField(verbose_name="属性", choices=attribute_choices) |
| |
| |
| |
2. 创建路由(项目-->url.py)
| |
| |
| |
| from bbc_list.views import login, home_page,order |
| |
| urlpatterns = [ |
| path("order/list/", order.order_list), |
| path("order/add/", order.order_add), |
| path("order/model/add/", order.order_model_add), |
| path("order/<int:nid>/update/", order.order_update), |
| path("order/<int:nid>/delete/", order.order_delete), |
| ] |
| |
| |
| |
| from django import forms |
| |
| from bbc_list import models |
| from bbc_list.utils.bootstrap import BootStrapModelForm |
| |
| |
| class OrderModelForm(BootStrapModelForm): |
| |
| order_id = forms.CharField(min_length=3, |
| label="订单号", |
| widget=forms.TextInput(attrs={"class": "form-control"})) |
| |
| class Meta: |
| model = models.Order |
| |
| fields = ["order_id", 'commodity_id', 'num', 'account', 'create_time', 'depart', 'attribute'] |
| |
4. 编写视图函数
| |
| |
| from django.shortcuts import render, redirect |
| |
| from bbc_list import models |
| from bbc_list.utils.form import OrderModelForm |
| |
| |
| def order_list(request): |
| """订单列表""" |
| |
| |
| queryset = models.Order.objects.all() |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| from bbc_list.utils.pagemtion import Pagination |
| page_object = Pagination(request, queryset, page_size=5) |
| context = { |
| "queryset": page_object.page_queryset, |
| "page_string": page_object.html() |
| } |
| |
| return render(request, "order_list.html", context) |
| |
| |
| def order_add(request): |
| """添加订单""" |
| if request.method == "GET": |
| context = { |
| 'attribute_choices': models.Order.attribute_choices, |
| 'depart_list': models.Home.objects.all() |
| } |
| return render(request, "order_add.html", context) |
| |
| |
| order_id = request.POST.get("order_id") |
| commodity_id = request.POST.get("commodity_id") |
| num = request.POST.get("num") |
| account = request.POST.get("account") |
| create_time = request.POST.get("create_time") |
| depart_id = request.POST.get("depart") |
| attribute = request.POST.get("attribute") |
| |
| |
| models.Order.objects.create(order_id=order_id, commodity_id=commodity_id, num=num, |
| account=account, create_time=create_time, depart_id=depart_id, attribute=attribute) |
| |
| |
| return redirect("/order/list/") |
| |
| |
| |
| |
| |
| def order_model_add(request): |
| """添加用户""" |
| """modelForm初始""" |
| if request.method == "GET": |
| form = OrderModelForm() |
| |
| return render(request, "order_model_add.html", {"form": form}) |
| |
| form = OrderModelForm(request.POST) |
| if form.is_valid(): |
| |
| |
| |
| form.save() |
| return redirect("/order/list/") |
| |
| |
| return render(request, "order_model_add.html", {"form": form}) |
| |
| |
| def order_update(request, nid): |
| """修改订单""" |
| row_object = models.Order.objects.filter(id=nid).first() |
| |
| |
| if request.method == "GET": |
| form = OrderModelForm(instance=row_object) |
| return render(request, "order_update.html", {"form": form}) |
| |
| form = OrderModelForm(data=request.POST, instance=row_object) |
| |
| if form.is_valid(): |
| form.save() |
| return redirect("/order/list/") |
| return render(request, "order_update.html", {"form": form}) |
| |
| |
| def order_delete(request, nid): |
| """删除订单""" |
| models.Order.objects.filter(id=nid).delete() |
| return redirect("/order/list/") |
5. 编写html
| # templates-->prder_list.html |
| |
| {% extends 'one.html' %} |
| |
| {% block content %} |
| <div class="container"> |
| <div style="margin-bottom:10px;"> |
| <a class="btn btn-success" href="/order/add/"> |
| <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> |
| 新建用户</a> |
| |
| <a class="btn btn-success" href="/order/model/add/"> |
| <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> |
| 新建用户ModelForm</a> |
| </div> |
| |
| <div class="panel panel-default"> |
| |
| <div class="panel-heading"> |
| <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> |
| 用户列表 |
| </div> |
| |
| |
| <table class="table table-bordered"> |
| <thead> |
| <tr> |
| <th>ID</th> |
| <th>订单</th> |
| <th>商品</th> |
| <th>数量</th> |
| <th>价格</th> |
| <th>下单日期</th> |
| <th>配送类型</th> |
| <th>属性</th> |
| <th>操作</th> |
| |
| </tr> |
| </thead> |
| <tbody> |
| {% for obj in queryset %} |
| <tr> |
| <th>{{ obj.id }}</th> |
| <td>{{ obj.order_id }}</td> |
| <td>{{ obj.commodity_id }}</td> |
| <td>{{ obj.num }}</td> |
| <td>{{ obj.account }}</td> |
| <td>{{ obj.create_time|date:"Y-m-d H:i:s" }}</td> |
| <td>{{ obj.get_attribute_display }}</td> |
| <td>{{ obj.depart.title }}</td> |
| <td> |
| <a class="btn btn-primary btn-xs" href="/order/{{ obj.id }}/update">编辑</a> |
| <a class="btn btn-danger btn-xs" href="/order/{{ obj.id }}/delete">删除</a> |
| </td> |
| </tr> |
| {% endfor %} |
| |
| </tbody> |
| </table> |
| </div> |
| |
| <ul class="pagination"> |
| {{ page_string }} |
| </ul> |
| </div> |
| |
| {% endblock %} |
| |
| # order_add.html |
| |
| {% extends 'one.html' %} |
| |
| {% load static %} |
| |
| {% block css %} |
| <link rel="stylesheet" href="{% static 'plugins/datetimepicker/css/bootstrap-datetimepicker.min.css '%}"> |
| |
| {% endblock %} |
| |
| {% block content %} |
| <div class="container"> |
| <div class="panel panel-default"> |
| <div class="panel-heading"> |
| <h3 class="panel-title">新建用户</h3> |
| </div> |
| <div class="panel-body"> |
| <form method="post"> |
| {% csrf_token %} |
| <div class="form-group"> |
| <label>订单号</label> |
| <input type="text" class="form-control" placeholder="订单号" name="order_id"> |
| </div> |
| <div class="form-group"> |
| <label>商品</label> |
| <input type="text" class="form-control" placeholder="商品" name="commodity_id"> |
| </div> |
| <div class="form-group"> |
| <label>数量</label> |
| <input type="text" class="form-control" placeholder="数量" name="num"> |
| </div> |
| <div class="form-group"> |
| <label>价格</label> |
| <input type="text" class="form-control" placeholder="价格" name="account"> |
| </div> |
| <div class="form-group"> |
| <label>下单时间</label> |
| <input id="dt" type="text" class="form-control" placeholder="下单时间" name="create_time"> |
| </div> |
| <div class="form-group"> |
| <label>属性</label> |
| <select class="form-control" name="attribute"> |
| {% for item in attribute_choices %} |
| <option value="{{ item.0 }}">{{ item.1 }}</option> |
| {% endfor %} |
| </select> |
| </div> |
| <div class="form-group"> |
| <label>配送类型</label> |
| <select class="form-control" name="depart"> |
| {% for item in depart_list %} |
| <option value="{{ item.id }}">{{ item.title }}</option> |
| {% endfor %} |
| </select> |
| </div> |
| |
| |
| <button type="submit" class="btn btn-primary">提交</button> |
| </form> |
| </div> |
| </div> |
| </div> |
| |
| |
| {% endblock %} |
| |
| {% block js %} |
| <script src="{% static 'plugins/datetimepicker/js/bootstrap-datetimepicker.js' %}"></script> |
| <script src="{% static 'plugins/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js' %}"></script> |
| <script> |
| $(function(){ |
| $('#dt').datetimepicker({ |
| language:'zh-CN', |
| format: 'yyyy-mm-dd', |
| minView: "month", |
| todayBtn:"true" |
| }); |
| }) |
| |
| |
| </script> |
| |
| {% endblock %} |
| |
| # order_modael_ad.html |
| |
| {% extends 'one.html' %} |
| |
| {% load static %} |
| |
| {% block css %} |
| <link rel="stylesheet" href="{% static 'plugins/datetimepicker/css/bootstrap-datetimepicker.min.css'%}"> |
| {% endblock %} |
| |
| {% block content %} |
| |
| <div class="content"> |
| <div class="panel panel-default"> |
| <div class="panel-heading"> |
| <h3 class="panel-title"> |
| 新建用户 |
| </h3> |
| </div> |
| <div class="panel-body"> |
| |
| <form method="post" novalidate> |
| {% csrf_token %} |
| |
| {% for field in form %} |
| <div class="form-group"> |
| <label>{{ field.label }}</label> |
| |
| {{ field }} |
| <span style="color:red;">{{ field.errors.0 }}</span> |
| </div> |
| {% endfor %} |
| |
| <button type="submit" class="btn btn-primary">提 交</button> |
| </form> |
| </div> |
| </div> |
| </div> |
| |
| {% endblock %} |
| |
| {% block js %} |
| <script src="{% static 'plugins/datetimepicker/js/bootstrap-datetimepicker.js' %}"></script> |
| <script src="{% static 'plugins/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js' %}"></script> |
| <script> |
| $(function(){ |
| $('#id_create_time').datetimepicker({ |
| language:'zh-CN', |
| format: 'yyyy-mm-dd', |
| minView: "month", |
| todayBtn:"true" |
| }); |
| }) |
| |
| </script> |
| |
| {% endblock %} |
| # order_update.html |
| |
| {% extends 'one.html' %} |
| |
| {% block content %} |
| <div class="container"> |
| <div class="panel panel-default"> |
| <div class="panel-heading"> |
| <h3 class="panel-title">编辑订单</h3> |
| </div> |
| <div class="panel-body"> |
| |
| <form method="post" novalidate> |
| {% csrf_token %} |
| |
| {% for field in form %} |
| <div class="form-group"> |
| <label>{{ field.label}}</label> |
| |
| {{ field }} |
| <span style="color:red;">{{ field.errors.0 }}</span> |
| </div> |
| {% endfor %} |
| |
| <button type="submit" class="btn btn-primary">提 交</button> |
| </form> |
| |
| </div> |
| </div> |
| </div> |
| |
| {% endblock %} |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现