parent
0faa6f8bd5
commit
946c8b4423
5 changed files with 154 additions and 14 deletions
@ -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}) |
||||
|
@ -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…
Reference in new issue