Python Django项目实例二
生活随笔
收集整理的這篇文章主要介紹了
Python Django项目实例二
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
管理員功能
查詢下架商品
商品下架前端代碼`
查詢修改商品
*查詢修改商品信息前端代碼
上架商品
上架前端代碼
數據報表
數據報表是使用Python的matplotlib第三方庫實現的故沒有前端代碼
接下來是全部的后端代碼
import refrom django.shortcuts import render, render_to_response,redirect from time import strftime import datetime from untitled4 import settings from .models import commodity,offtake import plotly as py import plotly.graph_objs as godef GoodsShelves(request):#進上架商品頁面if request.method == 'GET':return render(request, 'Goods_Shelves.html')def xiajia(request):#進下架商品頁面if request.method == 'GET':return render(request, 'xiajia.html')def QueryModity(request):#進查詢修改商品頁面if request.method == 'GET':return render(request, 'QueryModity.html')if request.method=='POST':data=request.POSTtry:commodit=commodity.objects.get(com_type=data.get('comtype'),com_name=data.get('comname'))return render(request,'QueryModity.html',{'commodity':commodit})except:return render(request, 'QueryModity.html')def queryxiajia(request):#進下架查詢頁面if request.method == 'GET':return render(request, 'xiajia.html')if request.method=='POST':data=request.POSTtry:commodit=commodity.objects.get(com_type=data.get('comtype'),com_name=data.get('comname'))return render(request,'xiajia.html',{'commodity':commodit})except:return render(request, 'xiajia.html') def GoodsShelvesfun(request):#上架商品方法if request.method=='POST':data =request.POSTimg = request.FILES['com_img']if data:com_id=data.get('com_id')#print(com_id)com_type = data.get('com_type')#print(com_type)com_name= data.get('com_name')com_num=data.get('com_num')com_inprice = data.get('com_inprice')com_outprice=data.get('com_outprice')com_discount=data.get('com_discount')imgdi='img1'+'/'+data.get('com_type')+'/' + img.namefname = settings.MEDIA_ROOT +'/'+'img1'+'/'+data.get('com_type')+'/' + img.name#print(fname)with open(fname, 'wb') as pic:for c in img.chunks():pic.write(c)try:commodity.objects.create(com_id=com_id,com_type=com_type,com_name=com_name,com_num=com_num,com_inprice=com_inprice,com_outprice=com_outprice,com_discount=com_discount,com_img=imgdi)#print('上架成功,繼續上架!')return render(request,'Goods_Shelves.html')except:#print('上架失敗!')return render(request,'Goods_Shelves.html')def QueryModityfun(request):#查詢、修改商品方法if request.method=='POST':data =request.POSTimg = request.FILES['com_img']if data:comid=data.get('com_id')#print(comid)comtype = data.get('com_type')#print(comtype)comname= data.get('com_name')comnum=data.get('com_num')cominprice = data.get('com_inprice')comoutprice=data.get('com_outprice')comdiscount=data.get('com_discount')try:com=commodity.objects.get(com_id=comid)if com.com_type != comtype:com.com_type=comtypeif com.com_name != comname:com.com_name=comnameif com.com_num != comnum:com.com_num = comnumif com.com_inprice != cominprice:com.com_inprice = cominpriceif com.com_outprice != comoutprice:com.com_outprice = comoutpriceif com.com_discount!=comdiscount:com.com_discount=comdiscountimg1 = 'img1' + '/' + data.get('com_type') + '/' + img.namefname = settings.MEDIA_ROOT + '/' + 'img1' + '/' + data.get('com_type') + '/' + img.name#print(fname)with open(fname, 'wb') as pic:for c in img.chunks():pic.write(c)if com.com_img!=img1:com.com_img=img1com.save()#保存修改#print('修改成功!')return redirect('/QueryModity')except:return redirect('/QueryModity')def xiajiafun(request):if request.method == 'POST':data = request.POST#print(data)if data:comname = data.get('com_name')print(comname)try:commodity.objects.filter(com_name= comname).delete()# print('修改成功!')return redirect('/xiajia')except:return redirect('/xiajia')def drawpicture(request):#數據報表方法,需要顯示各個商品售出數量,以及各個商品盈利,以每周每月為一個簇if request.method=="GET":list3 = []#print((datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d'))for j in commodity.objects.all():list2 = []x=eval(re.findall('\d+', j.com_outprice)[0])-eval(re.findall('\d+', j.com_inprice)[0])for i in range(7):list4=[]list1 = offtake.objects.filter(comm1=j.com_name,timenow=(datetime.datetime.now() - datetime.timedelta(days=i)).strftime('%Y-%m-%d')).first()if list1 != None:#print(list1.comm1)num=re.findall('\d+',list1.num)list4.insert(0,list1.comm1)list4.insert(1,num[0])list4.insert(2,str(x*eval(num[0])))list2.insert(i, list4)else:continueif list2!=None:list3.append(list2)else:continueprint(list3)i=0list5=[]list6=[]list7=[]list8=[]for li in list3:if li==[]:continueelse:for j in range(len(li)):if li[j][0] not in list5:list5.insert(i,li[j][0])list6.insert(i,int(li[j][1]))list7.insert(i,int(li[j][2]))i+=1else:x=list5.index(li[j][0])list6[x]=list6[x]+int(li[j][1])list7[x]=list7[x]+int(li[j][2])h=0for i in range(len(list7)):h += list7[i]for i in range(len(list5)):commodi=commodity.objects.get(com_name=list5[i])g=re.findall('\d+',commodi.com_num)list8.insert(i,int(g[0]))list5.append("總盈利")list7.append(h)trace_1= go.Bar(x=list5,y=list6,name="銷售量")trace_2=go.Bar(x=list5,y=list7,name="盈利(元)")trace_3=go.Bar(x=list5,y=list8,name="庫存")trace = [trace_1, trace_2,trace_3]# Layoutlayout = go.Layout(title='過去七天')# Figurefigure = go.Figure(data=trace, layout=layout)py.offline.plot(figure, filename="./myapp/templates/seedraw.html")return render(request,'seedraw.html')if request.method=="POST":timeweek=[]list3=[]for j in commodity.objects.all():list2 = []for i in range(7):list1=offtake.objects.filter(comm1=j.com_num,timenow=(datetime.datetime.now()-datetime.timedelta(days=i)).strftime('%Y-%m-%d'))if list1!=None:list2.insert(i,list1)else:continuelist3.append(list2)print(list3)#pyplt = py.offline.plot**還有個重要文件就是建立數據庫的相關文件**` 這個就是調數據庫問題的,順便給出我的數據庫圖片吧  這是跟數據庫相關的代碼 ```python from django.db import migrations, modelsclass Migration(migrations.Migration):initial = Truedependencies = []operations = [migrations.CreateModel(name='adminstor',fields=[('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),('uname', models.CharField(max_length=15)),('upassword', models.CharField(max_length=18)),],options={'db_table': 'adminstor',},),migrations.CreateModel(name='commodity',fields=[('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),('com_id', models.CharField(default='', max_length=6)),('com_type', models.CharField(default='', max_length=15)),('com_name', models.CharField(max_length=30)),('com_img', models.ImageField(upload_to='img1')),('com_num', models.CharField(max_length=15)),('com_inprice', models.CharField(default='', max_length=20)),('com_outprice', models.CharField(default='', max_length=15)),('com_discount', models.CharField(default='', max_length=15)),],options={'db_table': 'commodity_details',},),migrations.CreateModel(name='ident',fields=[('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),('u_name', models.CharField(max_length=18)),('edcom1', models.CharField(max_length=255)),('edcom2', models.CharField(max_length=255)),('edcom3', models.CharField(max_length=255)),('edcom4', models.CharField(max_length=255)),('edcom5', models.CharField(max_length=255)),('edcom6', models.CharField(max_length=255)),('edcom7', models.CharField(max_length=255)),],options={'db_table': 'ident',},),migrations.CreateModel(name='offtake',fields=[('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),('timenow', models.CharField(default='', max_length=30)),('comm1', models.CharField(default='', max_length=5000)),('comm2', models.CharField(default='', max_length=5000)),],options={'db_table': 'offtake',},),migrations.CreateModel(name='user',fields=[('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),('u_name', models.CharField(max_length=15)),('u_password', models.CharField(max_length=18)),('u_tellphone', models.CharField(max_length=15)),],options={'db_table': 'user',},),migrations.CreateModel(name='usershopcar',fields=[('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),('u_name', models.CharField(max_length=18)),('com1', models.CharField(max_length=18)),('num1', models.CharField(max_length=4)),('com2', models.CharField(max_length=18)),('num2', models.CharField(max_length=4)),('com3', models.CharField(max_length=18)),('num3', models.CharField(max_length=4)),('com4', models.CharField(max_length=18)),('num4', models.CharField(max_length=4)),('com5', models.CharField(max_length=18)),('num5', models.CharField(max_length=4)),('com6', models.CharField(max_length=18)),('num6', models.CharField(max_length=4)),('com7', models.CharField(max_length=18)),('num7', models.CharField(max_length=4)),],options={'db_table': 'usershopcar',},),]這是本人大三年級做的,時間過去許久,已將忘了大部分。抱歉只能貼碼了
本文原創
總結
以上是生活随笔為你收集整理的Python Django项目实例二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 神器-可视化分析之Basemap实战详解
- 下一篇: websocket python爬虫_p