如下步骤:
1.菜品信息数据表
2.定义模型model类
3.编写视图文件views
4.编写urls路由
3.编写模板文件
========================================================================
1.菜品信息数据表
本页面完成项目后台管理的菜品信息模块操作
(1). 菜品信息数据表:product
- 在数据库
osdb
中创建product
表,若此表已存在请跳过
CREATE TABLE `product` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '菜品id',
`shop_id` int(11) DEFAULT NULL COMMENT '店铺id',
`category_id` int(11) DEFAULT NULL COMMENT '菜品分类id',
`cover_pic` varchar(50) DEFAULT NULL COMMENT '菜品图片',
`name` varchar(50) DEFAULT NULL COMMENT '菜品名称',
`price` double(6,2) DEFAULT NULL COMMENT '单价',
`status` tinyint(4) DEFAULT NULL COMMENT '状态:1:正常 2:停售 9:删除',
`create_at` datetime DEFAULT NULL COMMENT '添加时间',
`update_at` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
(2). 定义模型Model类
- 进入
myadmin
应用目录中编辑:myobject/myadmin/models.py
模型文件
#菜品信息模型
class Product(models.Model):
shop_id = models.IntegerField() #店铺id
category_id = models.IntegerField() #菜品分类id
cover_pic = models.CharField(max_length=50) #菜品图片
name = models.CharField(max_length=50)#菜品名称
price = models.FloatField() #菜品单价
status = models.IntegerField(default=1) #状态:1正常/2停售/9删除
create_at = models.DateTimeField(default=datetime.now) #创建时间
update_at = models.DateTimeField(default=datetime.now) #修改时间
def toDict(self):
return {'id':self.id,'shop_id':self.shop_id,'category_id':self.category_id,'cover_pic':self.cover_pic,'name':self.name,'price':self.price,'status':self.status,'create_at':self.create_at.strftime('%Y-%m-%d %H:%M:%S'),'update_at':self.update_at.strftime('%Y-%m-%d %H:%M:%S')}
class Meta:
db_table = "product" # 更改表名
(3). 编辑视图文件
- 新建视图文件:myobject/myadmin/views/product.py 视图文件,并进行编辑
from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from django.core.paginator import Paginator
from datetime import datetime
import time,os
from myadmin.models import Product,Shop,Category
def index(request,pIndex=1):
'''浏览信息'''
smod = Product.objects
mywhere=[]
list = smod.filter(status__lt=9)
# 获取、判断并封装关keyword键搜索
kw = request.GET.get("keyword",None)
if kw:
# 查询店铺名称中只要含有关键字就可以
list = list.filter(name__contains=kw)
mywhere.append("keyword="+kw)
# 获取、判断并封装菜品类别category_id搜索条件
cid = request.GET.get('category_id','')
if cid != '':
list = list.filter(category_id=cid)
mywhere.append("category_id="+cid)
# 获取、判断并封装状态status搜索条件
status = request.GET.get('status','')
if status != '':
list = list.filter(status=status)
mywhere.append("status="+status)
list = list.order_by('id')
#执行分页处理
pIndex = int(pIndex)
page = Paginator(list,8) #以8条每页创建分页对象
maxpages = page.num_pages #最大页数
#判断页数是否越界
if pIndex > maxpages:
pIndex = maxpages
if pIndex < 1:
pIndex = 1
list2 = page.page(pIndex) #当前页数据
plist = page.page_range #页码数列表
#遍历信息,并获取对应的商铺名称,以shopname名封装;和菜品类别名称信息
for vo in list2:
sob = Shop.objects.get(id=vo.shop_id)
vo.shopname = sob.name
cob = Category.objects.get(id=vo.category_id)
vo.categoryname = cob.name
#封装信息加载模板输出
context = {"productlist":list2,'plist':plist,'pIndex':pIndex,'maxpages':maxpages,'mywhere':mywhere}
return render(request,"myadmin/product/index.html",context)
def add(request):
'''加载添加页面'''
slist = Shop.objects.values("id","name")
context={"shoplist":slist}
return render(request,"myadmin/product/add.html",context)
def insert(request):
'''执行添加'''
try:
#图片的上传处理
myfile = request.FILES.get("cover_pic",None)
if not myfile:
return HttpResponse("没有封面上传文件信息")
cover_pic = str(time.time())+"."+myfile.name.split('.').pop()
destination = open("./static/uploads/product/"+cover_pic,"wb+")
for chunk in myfile.chunks(): # 分块写入文件
destination.write(chunk)
destination.close()
#实例化model,封装信息,并执行添加
ob = Product()
ob.shop_id = request.POST['shop_id']
ob.category_id = requ