1,创建modelspy
from django.db import modelsclass Goods(models.Model): id = models.AutoField(primary_key=True) name = models.CharField('货物名称', default='', max_length=50) source = models.CharField('渠道', max_length=255, default='') status = models.CharField('状态', default='合格', max_length=50) create_time = models.DateTimeField('创建时间', auto_now_add=True) yxq = models.CharField('有效期', default='', max_length=50) description = models.TextField('商品描述')
def __str__(self): return self.name
class Meta: db_table = 'goods'
class Order(models.Model): id = models.AutoField(primary_key=True) goods = models.ForeignKey(Goods, on_delete=models.CASCADE) count = models.IntegerField('货物数量') status = models.CharField('状态', default='临时状态', max_length=50) owner = models.CharField('创建人', default='admin', max_length=50) create_time = models.DateTimeField('创建时间', auto_now_add=True) modify_time = models.DateTimeField('最后修改时间', auto_now=True) description = models.TextField('个人描述')
def __str__(self): return self.goods
class Meta: db_table = 'order'
class Transporter(models.Model): id = models.AutoField(primary_key=True) name = models.CharField('运输员', default='123', max_length=50) chepai = models.CharField('车牌', default='123', max_length=50) weight = models.CharField('载重', default='', max_length=50) status = models.CharField('状态', default='临时状态', max_length=50) create_time = models.DateTimeField('创建时间', auto_now_add=True) description = models.TextField('个人描述')
def __str__(self): return self.name
class Meta: db_table = 'transporter' class CangkuOrder(models.Model): id = models.AutoField(primary_key=True) goods = models.ForeignKey(Goods, on_delete=models.CASCADE) count = models.IntegerField('货物数量') price = models.IntegerField("价格",default=1) owner = models.CharField('创建人', default='admin', max_length=50) status = models.CharField('状态', default='入库', max_length=50) create_time = models.DateTimeField('创建时间', auto_now_add=True)
def __str__(self): return self.goods
class Meta: db_table = 'cangku_order'
class CarOrder(models.Model): id = models.AutoField(primary_key=True) transporter = models.ForeignKey(Transporter, on_delete=models.CASCADE) order = models.ForeignKey(CangkuOrder, on_delete=models.CASCADE) status = models.CharField('状态', default='临时状态', max_length=50) create_time = models.CharField('发车时间', default='123', max_length=50) sc_time = models.CharField('收车时间', default='123', max_length=50)
def __str__(self): return self.status
class Meta: db_table = 'car_order'
class Finance(models.Model): id = models.AutoField(primary_key=True) ck = models.ForeignKey(CangkuOrder, on_delete=models.CASCADE) price = models.FloatField("价格") status = models.CharField('状态', default='采购', max_length=50) create_time = models.DateTimeField('创建时间', auto_now_add=True)
def __str__(self): return self.ck
class Meta: db_table = 'finance' 执行makemigrations 和 migreate 生成 数据表 2.创建views.py import datetime import os from django.core.paginator import Paginator from django.db.models import Sum from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, JsonResponse from django.shortcuts import render from user.models import User from .models import *
workdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def login(req): """ 跳转登录 :param req: :return: """ return render(req, 'login.html')
def dashboard(request): total_user = len(User.objects.all()) total_sr = CangkuOrder.objects.filter(status='出库').aggregate(MyCulate=Sum('price'))['MyCulate'] total_zc = CangkuOrder.objects.filter(status='入库').aggregate(MyCulate=Sum('price'))['MyCulate'] total_cc = CangkuOrder.objects.count() total_ts = Transporter.objects.count() total_goods = Goods.objects.count() print(total_sr) date = datetime.datetime.today() month = date.month year = date.year day = date.day username = request.session['username'] current_day = str(year) + '-' + str(month) + "-" + str(day) return render(request, 'welcome.html',locals())
def register(req): """ 跳转注册 :param req: :return: """ return render(req, 'register.html')
def ls_order(req): """ 跳转注册 :param req: :return: """ goods = Goods.objects.all() role = int(req.session.get('role', 2)) username = req.session.get('username', 'admin') return render(req, 'ls_order.html', locals())
def change_status(request, status): response_data = {} user_id = request.POST.get('id') if status == 0: status = "不通过" else: status = "正式" Order.objects.filter(id=user_id).update( status=status) response_data['msg'] = 'success' return JsonResponse(response_data, status=201)
def zs_order(req): """ 跳转注册 :param req: :return: """ goods = Goods.objects.all() username = req.session.get('username', 'admin') return render(req, 'zs_order.html', locals())
def index(req): """ 跳转首页 :param req: :return: """ username = req.session['username'] total_user = len(User.objects.all()) date = datetime.datetime.today() month = date.month year = date.year return render(req, 'index.html', locals())
def login_out(req): """ 注销登录 :param req: :return: """ del req.session['username'] return HttpResponseRedirect('/')
def personal(req): username = req.session['username'] role_id = req.session['role'] user = User.objects.filter(name=username).first() return render(req, 'personal.html', locals())
def get_zs_order(request): """ 获取列表信息 | 模糊查询 :param request: :return: """ keyword = request.GET.get('name') page = request.GET.get("page", '') limit = request.GET.get("limit", '') role_id = request.GET.get('position', '') response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: results_obj = Order.objects.filter(status='正式').all() else: results_obj = Order.objects.filter(owner__contains=keyword, status='正式').all() paginator = Paginator(results_obj, limit) results = paginator.page(page) if results: for user in results: record = { "id": user.id, "name": user.goods.name, "count1": user.count, "status": user.status, "owner": user.owner, 'create_time': user.create_time.strftime('%Y-%m-%d %H:%m:%S'), "desc": user.description, } data.append(record) response_data['count'] = len(results_obj) response_data['data'] = data
return JsonResponse(response_data)
def get_ls_order(request): """ 获取列表信息 | 模糊查询 :param request: :return: """ keyword = request.GET.get('name') page = request.GET.get("page", '') limit = request.GET.get("limit", '') role_id = request.GET.get('position', '') response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: results_obj = Order.objects.filter(status='临时').all() else: results_obj = Order.objects.filter(owner__contains=keyword, status='临时').all() paginator = Paginator(results_obj, limit) results = paginator.page(page) if results: for user in results: record = { "id": user.id, "name": user.goods.name, "count1": user.count, "status": user.status, "owner": user.owner, 'create_time': user.create_time.strftime('%Y-%m-%d %H:%m:%S'), "desc": user.description, } data.append(record) response_data['count'] = len(results_obj) response_data['data'] = data
return JsonResponse(response_data)
def get_goods(request): """ 获取列表信息 | 模糊查询 :param request: :return: """ keyword = request.GET.get('name') page = request.GET.get("page", '') limit = request.GET.get("limit", '') role_id = request.GET.get('position', '') response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: results_obj = Goods.objects.all() else: results_obj = Goods.objects.filter(name__contains=keyword).all() paginator = Paginator(results_obj, limit) results = paginator.page(page) if results: for user in results: record = { "id": user.id, "name": user.name, "source": user.source, "status": user.status, "yxq": user.yxq, 'create_time': user.create_time.strftime('%Y-%m-%d %H:%m:%S'), "desc": user.description, } data.append(record) response_data['count'] = len(results_obj) response_data['data'] = data
return JsonResponse(response_data)
def goods(request): """ 跳转页面 """ username = request.session['username'] role = int(request.session['role']) user_id = request.session['user_id'] return render(request, 'goods.html', locals())
def add_goods(request): name = request.POST.get('name') source = request.POST.get('source') desc = request.POST.get('desc') yxq = request.POST.get('yxq')
goods_obj = Goods.objects.filter(name=name) if goods_obj: return JsonResponse({'message': '用户已存在,请直接登录'}, status=403) Goods.objects.create( name=name, description=desc, yxq=yxq, source=source, ) response_data = {'message': '注册成功'} return JsonResponse(response_data)
def add_order(request): goods_id = request.POST.get('goods') count = request.POST.get('count') owner = request.POST.get('username') Order.objects.create( goods_id=goods_id, count=count, owner=owner, status='临时' ) response_data = {'message': '注册成功'} return JsonResponse(response_data)
def edit_order(request): """ 修改信息 """ response_data = {} user_id = request.POST.get('id') goods_id = request.POST.get('goods') count = request.POST.get('count') Order.objects.filter(id=user_id).update( goods_id=goods_id, count=count) response_data['msg'] = 'success' return JsonResponse(response_data, status=201)
def del_order(request): """ 删除信息 """ user_id = request.POST.get('id') result = Order.objects.filter(id=user_id).first() try: if not result: response_data = {'error': '删除失败!', 'message': '找不到id为%s' % user_id} return JsonResponse(response_data, status=403) result.delete() response_data = {'message': '删除成功!'} return JsonResponse(response_data, status=201) except Exception as e: response_data = {'message': '删除失败!'} return JsonResponse(response_data, status=403)
def edit_goods(request): """ 修改信息 """ response_data = {} user_id = request.POST.get('id') name = request.POST.get('name') source = request.POST.get('source') yxq = request.POST.get('yxq') desc = request.POST.get('desc') Goods.objects.filter(id=user_id).update( name=name, source=source, description=desc, yxq=yxq) response_data['msg'] = 'success' return JsonResponse(response_data, status=201)
def del_goods(request): """ 删除信息 """ user_id = request.POST.get('id') result = Goods.objects.filter(id=user_id).first() try: if not result: response_data = {'error': '删除失败!', 'message': '找不到id为%s' % user_id} return JsonResponse(response_data, status=403) result.delete() response_data = {'message': '删除成功!'} return JsonResponse(response_data, status=201) except Exception as e: response_data = {'message': '删除失败!'} return JsonResponse(response_data, status=403)
def add_cangchu(request): goods = request.POST.get('goods') owner = request.POST.get('owner') status = request.POST.get('status') count = request.POST.get('count') price = request.POST.get('price') cangku_order = CangkuOrder.objects.create( goods_id=goods, owner=owner, status=status, count=count, price=price ) print(cangku_order.id) ts = Transporter.objects.filter(status='在岗').all()[0].id print('ts',ts) if status=='出库': CarOrder.objects.create( transporter_id=ts, order_id = cangku_order.id, status='正常', create_time ='待定', sc_time = '待定' ) Transporter.objects.filter(id=ts).update( status='送货' ) response_data = {'message': '注册成功'} return JsonResponse(response_data)
def edit_cangchu(request): """ 修改信息 """ response_data = {} user_id = request.POST.get('id') price = request.POST.get('price') count = request.POST.get('count') status = request.POST.get('status') CangkuOrder.objects.filter(id=user_id).update( price=price, status=status, count=count) response_data['msg'] = 'success' return JsonResponse(response_data, status=201)
def del_cangchu(request): """ 删除信息 """ user_id = request.POST.get('id') result = CangkuOrder.objects.filter(id=user_id).first() try: if not result: response_data = {'error': '删除失败!', 'message': '找不到id为%s' % user_id} return JsonResponse(response_data, status=403) result.delete() response_data = {'message': '删除成功!'} return JsonResponse(response_data, status=201) except Exception as e: response_data = {'message': '删除失败!'} return JsonResponse(response_data, status=403)
def get_cangchu(request): """ 获取列表信息 | 模糊查询 :param request: :return: """ keyword = request.GET.get('name') page = request.GET.get("page", '') limit = request.GET.get("limit", '') role_id = request.GET.get('position', '') response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: results_obj = CangkuOrder.objects.all() else: results_obj = CangkuOrder.objects.filter(owner__contains=keyword).all() paginator = Paginator(results_obj, limit) results = paginator.page(page) if results: for user in results: record = { "id": user.id, "name": user.goods.name, "count1": user.count, "price": user.price, "status": user.status, "owner": user.owner, 'create_time': user.create_time.strftime('%Y-%m-%d %H:%m:%S'), } data.append(record) response_data['count'] = len(results_obj) response_data['data'] = data
return JsonResponse(response_data)
def cangchu(request): """ 跳转页面 """ username = request.session['username'] role = int(request.session['role']) user_id = request.session['user_id'] goods = Goods.objects.all() return render(request, 'cangchu.html', locals())
def add_ts(request): name = request.POST.get('name') chepai = request.POST.get('chepai') status = request.POST.get('status') weight = request.POST.get('weight') desc = request.POST.get('desc') Transporter.objects.create( name=name, chepai=chepai, status=status, weight=weight, description=desc ) response_data = {'message': '注册成功'} return JsonResponse(response_data)
def edit_ts(request): """ 修改信息 """ response_data = {} user_id = request.POST.get('id') name = request.POST.get('name') desc = request.POST.get('desc') weight = request.POST.get('weight') chepai = request.POST.get('chepai') status = request.POST.get('status') Transporter.objects.filter(id=user_id).update( chepai=chepai, name=name, status=status, desc=desc, weight=weight) response_data['msg'] = 'success' return JsonResponse(response_data, status=201)
def del_ts(request): """ 删除信息 """ user_id = request.POST.get('id') result = Transporter.objects.filter(id=user_id).first() try: if not result: response_data = {'error': '删除失败!', 'message': '找不到id为%s' % user_id} return JsonResponse(response_data, status=403) result.delete() response_data = {'message': '删除成功!'} return JsonResponse(response_data, status=201) except Exception as e: response_data = {'message': '删除失败!'} return JsonResponse(response_data, status=403)
def get_ts(request): """ 获取列表信息 | 模糊查询 :param request: :return: """ keyword = request.GET.get('name') page = request.GET.get("page", '') limit = request.GET.get("limit", '') role_id = request.GET.get('position', '') response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: results_obj = Transporter.objects.all() else: results_obj = Transporter.objects.filter(owner__contains=keyword).all() paginator = Paginator(results_obj, limit) results = paginator.page(page) if results: for user in results: record = { "id": user.id, "name": user.name, "chepai": user.chepai, "weight": user.weight, "status": user.status, "desc": user.description, 'create_time': user.create_time.strftime('%Y-%m-%d %H:%m:%S'), } data.append(record) response_data['count'] = len(results_obj) response_data['data'] = data
return JsonResponse(response_data)
def ts(request): """ 跳转页面 """ username = request.session['username'] role = int(request.session['role']) user_id = request.session['user_id'] goods = Goods.objects.all() return render(request, 'transporter.html', locals())
def add_co(request): name = request.POST.get('name') chepai = request.POST.get('chepai') status = request.POST.get('status') weight = request.POST.get('weight') desc = request.POST.get('desc') Transporter.objects.create( name=name, chepai=chepai, status=status, weight=weight, description=desc ) response_data = {'message': '注册成功'} return JsonResponse(response_data)
def edit_co(request): """ 修改信息 """ response_data = {} user_id = request.POST.get('id') create_time = request.POST.get('create_time') sc_time = request.POST.get('sc_time') CarOrder.objects.filter(id=user_id).update(
create_time=create_time, sc_time=sc_time) response_data['msg'] = 'success' return JsonResponse(response_data, status=201)
def del_co(request): """ 删除信息 """ user_id = request.POST.get('id') result = CarOrder.objects.filter(id=user_id).first() try: if not result: response_data = {'error': '删除失败!', 'message': '找不到id为%s' % user_id} return JsonResponse(response_data, status=403) result.delete() response_data = {'message': '删除成功!'} return JsonResponse(response_data, status=201) except Exception as e: response_data = {'message': '删除失败!'} return JsonResponse(response_data, status=403)
def get_co(request): """ 获取列表信息 | 模糊查询 :param request: :return: """ keyword = request.GET.get('name') page = request.GET.get("page", '') limit = request.GET.get("limit", '') role_id = request.GET.get('position', '') response_data = {} response_data['code'] = 0 response_data['msg'] = '' data = [] if keyword is None: results_obj = CarOrder.objects.all() print(results_obj) else: results_obj = CarOrder.objects.filter(status__contains=keyword).all() paginator = Paginator(results_obj, limit) results = paginator.page(page) if results: for user in results: record = { "id": user.id, "order": user.order.id, "ts": user.transporter.name, "sc_time": user.sc_time, "status": user.status, 'create_time': user.create_time, } data.append(record) response_data['count'] = len(results_obj) response_data['data'] = data
return JsonResponse(response_data)
def car_order(request): """ 跳转页面 """ username = request.session['username'] role = int(request.session['role']) user_id = request.session['user_id'] goods = Goods.objects.all() return render(request, 'car_order.html', locals()) 3.配置urls.py from django.contrib import admin from django.urls import path, include from . import views
urlpatterns = [ path('', views.login), path('index', views.index), path('personal', views.personal), path('login_out', views.login_out), path('dashboard', views.dashboard), path('ls_order',views.ls_order), path('zs_order',views.zs_order), path('get_ls_order',views.get_ls_order), path('get_zs_order',views.get_zs_order), path('goods',views.goods), path('get_goods',views.get_goods), path('add_goods',views.add_goods), path('add_order',views.add_order), path('change_status/<status>',views.change_status), path('edit_order',views.edit_order), path('del_goods',views.del_goods), path('edit_goods',views.edit_goods), path('edit_cangchu',views.edit_cangchu), path('get_cangchu',views.get_cangchu), path('cangchu',views.cangchu), path('del_cangchu',views.del_cangchu), path('add_cangchu',views.add_cangchu), path('edit_ts', views.edit_ts), path('get_ts', views.get_ts), path('ts', views.ts), path('del_ts', views.del_ts), path('add_ts', views.add_ts), path('edit_co', views.edit_co), path('get_co', views.get_co), path('car_order', views.car_order), path('del_co', views.del_co), path('add_co', views.add_co),
] 4.使用模板语言与views进线关联 <!doctype html> <html class="x-admin-sm"> <head> <meta charset="UTF-8"> <title>企业物流管理系统</title> <meta name="renderer" content="webkit|ie-comp|ie-stand"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" /> <link rel="stylesheet" href="../static/css/font.css"> <link rel="stylesheet" href="../static/css/xadmin.css"> <!-- <link rel="stylesheet" href="./css/theme5.css"> --> <script src="../static/layui/layui.js" charset="utf-8"></script> <script type="text/javascript" src="../static/js/xadmin.js"></script>
<script> // 是否开启刷新记忆tab功能 // var is_remember = false; </script> </head> <body class="index"> <!-- 顶部开始 --> <div class="container"> <div class="logo"> <a href="#">企业物流管理系统</a></div> <div class="left_open"> <a><i title="展开左侧栏" class="iconfont"></i></a> </div>
<ul class="layui-nav right" lay-filter=""> <li class="layui-nav-item"> <a href="javascript:;">{{ username }}</a> <dl class="layui-nav-child"> <!-- 二级菜单 --> <dd> <a onclick="xadmin.open('个人信息','/personal')">个人信息</a></dd> <dd> <a href="/login_out">注销登录</a></dd> </dl> </li> <li class="layui-nav-item to-index"> <a href="/index">前台首页</a></li> </ul> </div> <div class="left-nav"> <div id="side-nav"> <ul id="nav">
<li> <a href="javascript:;"> <i class="iconfont left-nav-li" lay-tips="订单管理"></i> <cite>订单管理</cite> <i class="iconfont nav_right"></i></a> <ul class="sub-menu"> <li> <a onclick="xadmin.add_tab('临时订单','/ls_order')"> <i class="iconfont"></i> <cite>临时订单</cite></a> </li> <li> <a onclick="xadmin.add_tab('正式订单','/zs_order')"> <i class="iconfont"></i> <cite>正式订单</cite></a> </li> </ul> </li> <li> <a href="javascript:;"> <i class="iconfont left-nav-li" lay-tips="货物管理"></i> <cite>货物管理</cite> <i class="iconfont nav_right"></i></a> <ul class="sub-menu"> <li> <a onclick="xadmin.add_tab('货物列表','/goods')"> <i class="iconfont"></i> <cite>货物列表</cite></a> </li> </ul> </li> <li> <a href="javascript:;"> <i class="iconfont left-nav-li" lay-tips="仓储管理"></i> <cite>仓储管理</cite> <i class="iconfont nav_right"></i></a> <ul class="sub-menu"> <li> <a onclick="xadmin.add_tab('仓储列表','/cangchu')"> <i class="iconfont"></i> <cite>仓储列表</cite></a> </li> </ul> </li> <li> <a href="javascript:;"> <i class="iconfont left-nav-li" lay-tips="运输管理"></i> <cite>运输管理</cite> <i class="iconfont nav_right"></i></a> <ul class="sub-menu"> <li> <a onclick="xadmin.add_tab('运输员列表','/ts')"> <i class="iconfont"></i> <cite>运输员列表</cite></a> </li> <li> <a onclick="xadmin.add_tab('运输订单','/car_order')"> <i class="iconfont"></i> <cite>运输订单</cite></a> </li> </ul> </li>
<li> <a href="javascript:;"> <i class="iconfont left-nav-li" lay-tips="人事管理"></i> <cite>人事管理</cite> <i class="iconfont nav_right"></i></a> <ul class="sub-menu"> <li> <a onclick="xadmin.add_tab('用户列表','/user/user')"> <i class="iconfont"></i> <cite>用户列表</cite></a> </li> <li> <a onclick="xadmin.add_tab('个人信息','/personal')"> <i class="iconfont"></i> <cite>个人信息</cite></a> </li> </ul> </li> </ul> </div> </div> <!-- <div class="x-slide_left"></div> --> <!-- 左侧菜单结束 --> <!-- 右侧主体开始 --> <div class="page-content"> <div class="layui-tab tab" lay-filter="xbs_tab" lay-allowclose="false"> <ul class="layui-tab-title"> <li class="home"> <i class="layui-icon"></i>控制台</li></ul> <div class="layui-unselect layui-form-select layui-form-selected" id="tab_right"> <dl> <dd data-type="this">关闭当前</dd> <dd data-type="other">关闭其它</dd> <dd data-type="all">关闭全部</dd></dl> </div> <div class="layui-tab-content"> <div class="layui-tab-item layui-show"> <iframe src='/dashboard' frameborder="0" scrolling="yes" class="x-iframe"></iframe> </div> </div> <div id="tab_show"></div> </div> </div> <div class="page-content-bg"></div> <style id="theme_style"></style> </body>
</html> 5.效果
参考gitcode:
https://github.com/2454760302hui/TestProject
标签:get,request,django,user,版本,物流,response,data,id From: https://www.cnblogs.com/yanhuidj/p/18118669