完成cate相关代码

main
roger_home_pc 1 year ago
parent 0faa6f8bd5
commit 946c8b4423
  1. 15
      apps/goods/forms.py
  2. 24
      apps/goods/urls.py
  3. 56
      apps/goods/views.py
  4. 4
      myshop_back/urls.py
  5. 69
      templates/goods/cate_add.html

@ -9,7 +9,7 @@ class GoodsCateForm(forms.Form):
name = forms.CharField(label='分类名称', min_length=2, widget=forms.widgets.TextInput(
attrs={'class': 'form-control', 'placeholder': '请输入分类名称'}),
error_messages={'required': '分类名称不能为空!', 'min_length': '长度最小2位'})
parent_id = forms.CharField(label='选择父类', max_length=20, required=True,
parent_id = forms.CharField(label='选择父类', max_length=20, required=False,
widget=forms.widgets.Select(
attrs={'class': 'form-control custom-select', 'placeholder': '选择父类'}),
error_messages={'required': '请选择父类'})
@ -24,15 +24,20 @@ class GoodsCateForm(forms.Form):
self.alist = [('', '请选择...')]
self.fields['parent_id'].widget.choices = self.bind_data(cate_all, 0, 1)
def bind_data(self, datas, _id, n):
def bind_data(self, datas_all, _id, n):
if _id == 0:
datas = datas.filter(parent__isnull=True)
print(111111111111)
datas = datas_all.filter(parent__isnull=True)
print(datas)
else:
datas = datas.filter(parent_id=id)
print(222222222222)
datas = datas_all.filter(parent_id=_id)
print(datas)
for data in datas:
print(data)
self.alist.append((data.id, self.space_length(n) + data.name))
self.bind_data(datas, data.id, n+2)
self.bind_data(datas_all, data.id, n+2)
print(self.alist)
return self.alist

@ -0,0 +1,24 @@
"""
URL configuration for myshop_back project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
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'),
]

@ -1,3 +1,57 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.views.generic.base import View
from apps.goods.models import *
from apps.goods.forms import *
# Create your views here.
class GoodCateView(View):
def get(self, request):
cates = GoodsCategory.objects.all()
return render(request, 'goods/cate_index.html', {'cates': cates})
def post(self, request):
pass
class GoodCateAddView(View):
def __init__(self):
self.alist = []
def bind_data(self, data, _id, n):
if id == 0:
data = data.filter(parent__isnull=True)
else:
data = data.filter(parent=_id)
for d in data:
print(d)
self.alist[d.id] = self.space_length(n) + d.name
self.bind_data(d, d.id, n + 2)
return self.alist
def space_length(self, i):
space = ''
for j in range(1, i):
space += "  "
return space + "|--"
def get(self, request):
form_obj = GoodsCateForm()
return render(request, 'goods/cate_add.html', {'form_obj': form_obj})
def post(self, request):
form_obj = GoodsCateForm(request.POST, request.FILES)
if form_obj.is_valid():
name = request.POST.get('name', '')
cates = GoodsCategory.objects.filter(name=name)
if cates:
info = '分类已经存在'
else:
cate = GoodsCategory.objects.create(**form_obj.cleaned_data)
return redirect('cate_index')
return render(request, 'goods/cate_add.html', {'form_obj': form_obj, 'info': info})
else:
errors = form_obj.errors
print(errors)
return render(request, 'goods/cate_add.html', {'form_obj': form_obj, 'info': errors})

@ -17,9 +17,9 @@ Including another URLconf
from django.contrib import admin
from django.urls import path, re_path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('user/', include('apps.user.urls')),
path('basic/', include('apps.basic.urls'))
path('basic/', include('apps.basic.urls')),
path('goods/', include('apps.goods.urls'))
]

@ -1,7 +1,64 @@
{% extends 'shop/base.html' %}
{% load static %}
{% block ext_css %}
{% endblock %}
{% block content %}
{% endblock %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>新增商品分类</title>
<link rel="stylesheet" href="{% static '/dist/css/adminlte.min.css' %}">
</head>
<body>
<div class="wrapper">
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="row-sm-12">
<ol class="breadcrumb float-sm-left">
<li class="breadcrumb-item"><a href="#">商品分类</a></li>
<li class="breadcrumb-item active">商品分类增加</li>
</ol>
</div>
</div>
</div>
</section>
<section class="content">
<div class="row">
<div class="col-md-12">
<form novalidate enctype="multipart/form-data" action="" method="post" class="form-horizontal">
{% csrf_token %}
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">添加商品分类</h3>
</div>
<div class="card-body">
<div class="form-group">
<label for="{{ form_obj.name.if_for_label }}">{{ form_obj.name.label }}</label>
{{ form_obj.name }}
</div>
<div class="form-group">
<label for="{{ form_obj.parent_id.id_for_label }}">{{ form_obj.parent_id.label }}</label>
{{ form_obj.parent_id }}
</div>
<div class="form-group">
<label for="{{ form_obj.sort.id_for_label }}">{{ form_obj.sort.label }}</label>
{{ form_obj.sort }}
</div>
<div class="form-group">
<label for="{{ form_obj.logo.id_for_label }}">{{ form_obj.logo.label }}</label>
<div class="custom-file">
{{ form_obj.logo }}
<label class="custom-file-label" for="customFile">选择图片</label>
</div>
</div>
<div class="card-footer">
<a href="#" class="btn btn-success">取消</a>
<button type="submit" class="btn btn-primary">提交</button>
{{ info }}
</div>
</div>
</div>
</form>
</div>
</div>
</section>
</div>
</body>
</html>
Loading…
Cancel
Save