代码写完了,但是需要调试

main
roger_home_pc 1 year ago
parent 6b0a1c6ac7
commit 0f0a525915
  1. 2
      apps/goods/urls.py
  2. 97
      apps/goods/views.py

@ -21,4 +21,6 @@ from apps.goods import views
urlpatterns = [
path('cate_index/', views.GoodCateView.as_view(), name='cate_index'),
path('cate_add/', views.GoodCateAddView.as_view(), name='cate_add'),
path('index/', views.GoodView.as_view(), name='good_index'),
path('add/', views.GoodsAddView.as_view(), name='good_add')
]

@ -1,4 +1,5 @@
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, reverse
from django.http.response import JsonResponse
from django.views.generic.base import View
from apps.goods.models import *
@ -55,3 +56,97 @@ class GoodCateAddView(View):
errors = form_obj.errors
print(errors)
return render(request, 'goods/cate_add.html', {'form_obj': form_obj, 'info': errors})
class GoodView(View):
def __init__(self):
self.alist = {}
def bind_data(self, data, _id, n):
data = GoodsCategory.objects.filter(parent_id=_id)
for d in data:
self.alist[data.id] = self.spacelength(n) + d.name
self.bind_data(data, d.id, n + 2)
return self.alist
def spacelength(self, i):
space = ''
for j in range(1, i):
space += "  "
return space + "|--"
def get(self, request):
cates_all = GoodsCategory.objects.all()
cates = self.bind_data(cates_all, 0, 1)
return render(request, 'goods/goods_index.html', {'cates': cates})
def post(self, request):
pass
class GoodsAddView(View):
def __init__(self):
self.alist = {}
def binddata(self, datas, id, n):
if id == 0:
datas = GoodsCategory.objects.filter(parent__isnull=True)
else:
datas = GoodsCategory.objects.filter(parent_id=id)
for data in datas:
self.alist[data.id] = self.spacelength(n) + data.name
self.binddata(datas, data.id, n + 2)
return self.alist
def spacelength(self, i):
space = ''
for j in range(1, i):
space += "  "
return space + "|--"
def get(self, request):
cates_all = GoodsCategory.objects.all()
cates = self.binddata(cates_all, 0, 1)
return render(request, 'goods/goods_add.html', {'cates': cates})
def post(self, request):
name = request.POST.get('name', '')
parent_id = request.POST.get('parent_id', '')
market_price = request.POST.get('market_price', '0')
price = request.POST.get('price', '0')
goods_desc = request.POST.get('goods_desc', '')
main_img = request.POST.get('main_img', '')
message = '字段需要填写'
if not name:
message = '请输入姓名'
return redirect(reverse('index'))
def ajax_goods(request):
cate_id = request.POST.get('cate_id', '')
goodname = request.POST.get('goodname', '')
status = request.POST.get('status')
search_dict = dict()
if cate_id:
search_dict['category'] = cate_id
if goodname:
search_dict['name__contains'] = goodname
if status:
search_dict['status'] = status
page_size = 2
page = int(request.GET['page'])
total = Goods.objects.filter(**search_dict).count()
goods = Goods.objects.filter(**search_dict).order_by('-id')[(page - 1) * page_size: page * page_size]
rows = []
datas = {'total': total, 'row': rows}
for good in goods:
rows.append({
'id': good.id,
'name': good.name,
'market_price': good.market_price,
'price': good.price,
'category_id': good.category.name,
'click_num': good.click_num,
'amount': good.amount,
})
return JsonResponse(datas, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 4})

Loading…
Cancel
Save