欢迎来到我的网站
Python-Web 09结合实训
Python-Web 09结合实训

Python-Web 09结合实训

准备工作(共享、数据)
共享python_web文件夹
安装数据库及客户端

任务1:创建三个模型(Ptype,SubType,Product)
*步骤1:创建模型—-》index/models.py
class Ptype(models.Model):
pid=models.AutoField(‘pid’,primary_key=True)
ptName=models.CharField(‘分类名’,max_length=20)

class SubType(models.Model):
sid=models.AutoField(‘sid’,primary_key=True)
subName = models.CharField(‘系列’, max_length=20)
ptype=models.ForeignKey(Ptype,on_delete=models.CASCADE,verbose_name=’分类’)

class Product(models.Model):
id=models.AutoField(‘id’,primary_key=True)
pName = models.CharField(‘系列’, max_length=20)
weight = models.CharField(‘重量’, max_length=20)
pSize = models.CharField(‘尺寸’, max_length=30)
price = models.IntegerField(‘价格’, default=9999)
imageURL = models.CharField(‘产品图片’, max_length=60, default=”)
subtpye = models.ForeignKey(SubType, on_delete=models.CASCADE, verbose_name=’系列ID’)
*步骤2:完成数据模型到表的迁移
python3 manage.py makemigrations
python3 manage.py migrate

任务2:完成后台管理功能
*步骤1:分析功能及实现页面(增、删、改、查)

页面1:所有商品的展示列表及操作链接—》查
页面2:添加商品—-》增、改
URL:删除商品—-》删

*步骤2:前置条件:分类—-》系列
手动在数据库添加数据,解决数据依赖关系
分类:手机、笔记本电脑、平板&穿戴、智能家居、配件
系列:Mate系列、P系列

*步骤3:完成“商品的展示列表及操作链接”页面
3.1创建模板—-》productList.html

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>华为商城–后台管理</title>
</head>
<body>
<h2>华为商城–商品列表</h2>
<hr/>
<a href=”/index/productEdit/”>添加商品….</a>
<br/>
<table border=”1px”>
<tr><td>ID</td><td>名称</td><td>类型</td><td>操作</td></tr>
{% for product in productList %}
<tr><td>product.id</td><td>product.pName</td><td>product.subtpye.subName</td><td><a href=”/index/productEdit/?id={{product.id}}”>[修改]</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=”/index/delProduct/?id={{product.id}}”>[删除]</a></td></tr>
{% endfor %}
</table>
</body>
</html>
3.2 添加路由
path(‘productList/’,views.getProductList),
3.3编写视图处理函数
from index.models import Ptype,Product,SubType
def getProductList(request):
#所有商品的展示列表—>数据库
context={}
context[‘productList’]=Product.objects.all()
return render(request,”productList.html”,context)
*步骤4:完成“添加商品”页面
4.1创建模板—-》addProduct.html
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>华为商城–后台管理</title>
</head>
<body>
<h2>华为商城–商品信息处理</h2>
<hr/>
<form method=”post” action=”#”>
{% csrf_token %}
<table>
<tr><td>商品名称:</td><td><input type=”text” name=”pName” value=”{{product.pName}}”></td></tr>
<tr><td>重量:</td><td><input type=”text” name=”weight” value=”{{product.weight}}”></td></tr>
<tr><td>尺寸:</td><td><input type=”text” name=”pSize” value=”{{product.pSize}}”></td></tr>
<tr><td>价格:</td><td><input type=”text” name=”price” value=”{{product.price}}”></td></tr>
<tr><td>图片:</td><td><input type=”text” name=”imageURL” value=”{{product.imageURL}}”></td></tr>
<tr><td>系列ID:</td><td><input type=”text” name=”subtpyeID” value=”{{product.subtpye_id}}”></td></tr>
<tr><td>&nbsp;</td><td><input type=”submit” value=”保存”></td></tr>
</table>
</form>
</body>
</html>

4.2 添加路由
path(‘productEdit/’,views.productEdit),
4.3编写视图处理函数
def productEdit(request):
#商品信息处理—>添加或修改商品信息
if request.method==”GET”:
context={}
try:
#修改商品信息
pid = request.GET[“id”]
p_obj = Product.objects.get(id=pid)
context[“product”]=p_obj
except Exception:
pass
return render(request,”addProduct.html”,context)
#POST—>提交了数据(添加,修改)
try:
pid=request.GET[“id”]
p_obj=Product.objects.get(id=pid)
except Exception:
p_obj=Product()
p_obj.pName=request.POST[“pName”]
p_obj.weight=request.POST[“weight”]
p_obj.pSize=request.POST[“pSize”]
p_obj.price=int(request.POST[“price”])
p_obj.imageURL=”/static/img/”+request.POST[“imageURL”]
p_obj.subtpye_id=int(request.POST[“subtpyeID”])
p_obj.save()
return redirect(“/index/productList/”)

*步骤5:完成“删除商品”URL
5.1 添加路由
path(‘delProduct/’,views.delProduct),
5.2编写视图处理函数
def delProduct(request):
try:
pid = request.GET[“id”]
Product.objects.get(id=pid).delete()
except Exception:
pass
return redirect(“/index/productList/”)

任务3:实现“华为商城”的展示页面
*步骤1:创建模板—-》huawei.html

<!DOCTYPE html>
<html lang=”zh”>
<head>
<meta charset=”UTF-8″>
<link rel=”stylesheet” type=”text/css” href=”/static/css/huawei.css”>
<title>华为商城</title>
</head>
<body>
<div id=”top”>
<ul>
<li style=”width:260px;padding-top:10px;”><img src=”/static/img/logo.png”></li>
{% for ptype in pTypeList %}
<li>{{ ptype.ptName }}</li>
{% endfor %}
</ul>
</div>
<!– 商城导航 –>
<div id=”nav” class=”contextCenter”>
<div id=”nav01″ >
<a href=”./index.html”>首页</a>
&nbsp;&gt;&gt;手机
</div>
<div id=”nav02″>
<ul>
<li style=”width:40px;”>分类:</li>
{% for subtype in subTypeList %}
<li><a href=”/index/huawei/?sid={{subtype.sid}}”>{{ subtype.subName }}</a></li>
{% endfor %}
</ul>
</div>
<div id=”nav03″>
<ul>
<li style=”width:40px;”>排序:</li>
<li>上架时间</li>
<li>价格</li>
<li>评价</li>
</ul>
</div>
</div>
<!– 商品列表 –>
<div id=”list” class=”contextCenter”>
<ul>
{% for product in productList %}
<li>
<div class=”listItem01″><img src=”{{product.imageURL}}” style=”width:180px;height:180px;”></div>
<div class=”listItem02″>{{product.pName}}</div>
<div class=”listItem03″>¥{{product.price}}</div>
<div class=”listItem04″><span>选购</span><span>26289评价</span></div>
</li>
{% endfor %}

</ul>
</div>
<!– 页脚 –>
<div id=”footer” class=”contextCenter”></div>
</body>
</html>

*步骤2:添加路由
path(‘huawei/’,views.getHuawei),
*步骤3:编写视图处理函数
def getHuawei(request):
try:
pid = request.GET[“pid”]
except Exception:
pid=1
try:
sid = request.GET[“sid”]
except Exception:
sid = 1
context={}
context[“pTypeList”]=Ptype.objects.all()
context[“subTypeList”]=SubType.objects.filter(ptype_id=pid)
context[“productList”]=Product.objects.filter(subtpye_id=sid)
return render(request,”huawei.html”,context)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注