7. 项目实战后台之商品类别信息管理
优质
小牛编辑
131浏览
2023-12-01
本页面完成项目后台管理的商品类别信息模块操作
(1). 商品类别信息数据表:type
- 在数据库
shopdb
中创建type表,若此表已存在请跳过
CREATE TABLE `type` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`pid` int(11) unsigned DEFAULT '0',
`path` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
(2). 定义模型Model类
- 进入
common
应用目录中编辑:myobject/common/models.py
模型文件
from django.db import models
#商品类别信息模型
class Types(models.Model):
name = models.CharField(max_length=32)
pid = models.IntegerField(default=0)
path = models.CharField(max_length=255)
class Meta:
db_table = "type" # 更改表名
(3). 项目urls路由信息配置
- 打开根路由文件:myobject/myadmin/urls.py路由文件,编辑路由配置信息
from django.conf.urls import url
from myadmin.views import index,users,type
urlpatterns = [
...
# 后台商品类别信息管理
url(r'^type$', type.index, name="myadmin_type_index"),
url(r'^type/add/(?P<tid>[0-9]+)$', type.add, name="myadmin_type_add"),
url(r'^type/insert$', type.insert, name="myadmin_type_insert"),
url(r'^type/del/(?P<tid>[0-9]+)$', type.delete, name="myadmin_type_del"),
url(r'^type/edit/(?P<tid>[0-9]+)$', type.edit, name="myadmin_type_edit"),
url(r'^type/update/(?P<tid>[0-9]+)$', type.update, name="myadmin_type_update"),
]
(4). 编辑视图文件
- 新建视图文件:myobject/myadmin/views/type.py 视图文件,并进行编辑
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from common.models import Types
# 浏览商品类别信息
def index(request):
# 执行数据查询,并放置到模板中
list = Types.objects.extra(select = {'_has':'concat(path,id)'}).order_by('_has')
# 遍历查询结果,为每个结果对象追加一个pname属性,目的用于缩进标题
for ob in list:
ob.pname ='. . . '*(ob.path.count(',')-1)
# print(list[0].__dict__)
context = {"typeslist":list}
return render(request,'myadmin/type/index.html',context)
# 商品类别信息添加表单
def add(request,tid):
# 获取父类别信息,若没有则默认为根类别信息
if tid == '0':
context = {'pid':0,'path':'0,','name':'根类别'}
else:
ob = Types.objects.get(id=tid)
context = {'pid':ob.id,'path':ob.path+str(ob.id)+',','name':ob.name}
return render(request,'myadmin/type/add.html',context)
#执行商品类别信息添加
def insert(request):
try:
ob = Types()
ob.name = request.POST['name']
ob.pid = request.POST['pid']
ob.path = request.POST['path']
ob.save()
context = {'info':'添加成功!'}
except Exception as err:
print(err)
context = {'info':'添加失败!'}
return render(request,"myadmin/info.html",context)
# 执行商品类别信息删除
def delete(request,tid):
try:
# 获取被删除商品的子类别信息量,若有数据,就禁止删除当前类别
row = Types.objects.filter(pid=tid).count()
if row > 0:
context = {'info':'删除失败:此类别下还有子类别!'}
return render(request,"myadmin/info.html",context)
ob = Types.objects.get(id=tid)
ob.delete()
context = {'info':'删除成功!'}
except Exception as err:
print(err)
context = {'info':'删除失败!'}
return render(request,"myadmin/info.html",context)
# 打开商品类别信息编辑表单
def edit(request,tid):
try:
ob = Types.objects.get(id=tid)
context = {'type':ob}
return render(request,"myadmin/type/edit.html",context)
except Exception as err:
print(err)
context = {'info':'没有找到要修改的信息!'}
return render(request,"myadmin/info.html",context)
# 执行商品类别信息编辑
def update(request,tid):
try:
ob = Types.objects.get(id=tid)
ob.name = request.POST['name']
ob.save()
context = {'info':'修改成功!'}
except Exception as err:
print(err)
context = {'info':'修改失败!'}
return render(request,"myadmin/info.html",context)
(5). 编写模板文件
- 5.1. 打开父类模板:/templates/myadmin/base.html ,编辑导航栏代码
...
<li class="nav-header">
商品类别管理
</li>
<li>
<a href="{% url 'myadmin_type_index' %}"><i class="icon-envelope"></i> 浏览商品类别</a>
</li>
<li>
<a href="{% url 'myadmin_type_add' 0 %}"><i class="icon-file"></i> 添加商品类别</a>
</li>
...
- 5.2. 后台商品类别信息浏览页模板:/templates/myadmin/type/index.html
{% extends "myadmin/base.html" %}
{% block mainbody %}
<h4>
商品类别信息管理
</h4>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID号</th>
<th>类别名称</th>
<th>父类别id</th>
<th>路径</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for vo in typeslist %}
<tr>
<td>{{ vo.id }}</td>
<td>{{ vo.pname}}|-- {{ vo.name }}</td>
<td>{{ vo.pid }}</td>
<td>{{ vo.path }}</td>
<td width="30%">
<a href="{% url 'myadmin_type_add' vo.id %}" class="view-link">添加子类别</a>
<a href="{% url 'myadmin_type_del' vo.id %}" class="view-link">删除</a>
<a href="{% url 'myadmin_type_edit' vo.id %}" class="view-link">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="pagination">
<ul>
<li class="disabled">
<a href="#">«</a>
</li>
<li class="active">
<a href="#">1</a>
</li>
<li>
<a href="#">2</a>
</li>
<li>
<a href="#">3</a>
</li>
<li>
<a href="#">4</a>
</li>
<li>
<a href="#">»</a>
</li>
</ul>
</div>
{% endblock %}
- 5.3. 后台商品类别信息添加表单页模板:/templates/myadmin/type/add.html
{% extends "myadmin/base.html" %}
{% block mainbody %}
<h3>
商品类别信息管理
</h3>
<form id="edit-profile" action="{% url 'myadmin_type_insert' %}" class="form-horizontal" method="post">
<input type="hidden" name="pid" value="{{ pid }}"/>
<input type="hidden" name="path" value="{{ path }}"/>
{% csrf_token %}
<fieldset>
<legend>添加商品类别信息</legend>
<div class="control-group">
<label class="control-label" for="input01">父类别名称:</label>
<div class="controls">
<input type="text" name="pname" class="input-xlarge" id="input01" value="" placeholder="{{name}}"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01">类别名称:</label>
<div class="controls">
<input type="text" name="name" class="input-xlarge" id="input01"/>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">添加</button> <button type="reset" class="btn">重置</button>
</div>
</fieldset>
</form>
{% endblock %}
- 5.4. 后台商品信息编辑模板:/templates/myadmin/type/edit.html
{% extends "myadmin/base.html" %}
{% block mainbody %}
<h3>
商品类别信息管理
</h3>
<form id="edit-profile" action="{% url 'myadmin_type_update' type.id %}" class="form-horizontal" method="post">
{% csrf_token %}
<fieldset>
<legend>编辑商品类别信息</legend>
<div class="control-group">
<label class="control-label" for="input01">类别名称:</label>
<div class="controls">
<input type="text" name="name" class="input-xlarge" id="input01" value="{{ type.name }}" />
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">保存</button> <button type="reset" class="btn">重置</button>
</div>
</fieldset>
</form>
{% endblock %}
6 运行测试
- 在项目根目录下启动服务,并使用浏览器访问测试:http://localhost:8000/myadmin
[root@localhost myobject]# pwd
/python/myobject
[root@localhost myobject]# ls
manage.py myadmin myobject myweb static templates
[root@localhost myobject]# python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
April 07, 2018 - 16:29:36
Django version 1.11, using settings 'myobject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
^C[root@localhost myobject]#