一 *** 循环列表中信息到页面
1.views.py
from django.shortcutsimport render,reverse
from django.httpimport HttpResponseRedirect
# Create your views here.
#商品列表
def list(request):
#指定跳转到某一个路由
#return HttpResponseRedirect(reverse("goods:list"))
goods={"info":[
{"id":1,"name":"computer","price":5000,"discount":4500},
{"id":2, "name":"notebook", "price":2500, "discount":2000},
{"id":3, "name":"tvstation", "price":6000 , "discount":5500},
{"id":4, "name":"brigdge", "price":8000, "discount":7500},
{"id":5, "name":"phone", "price":9000, "discount":8500},
]}
return render(request,"list.html",context={"goods":goods})
#商品详情
def detail(request):
print(request.GET)
id=request.GET['id']#等同于 id=request.GET.get('id')
print(id)
return render(request,"detail.html")
def details(request,id):
print(id)
#通过函数名反转路径
print(reverse("goods:details"))
return render(request,"detail.html")
2.urls.py
![](https://img.haomeiwen.com/i22651072/02e610dc93d7331f.png)
3.list.html
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>商品列表
<h1>商品列表
{%for i,j in goods.items %}
商品信息*{{ i }}
商品列表*
{%for o in j %}
商品id*{{o.id }}
商品名称*{{o.name }}
商品价格*{{o.price }}
商品折扣*{{o.discount }}
<a href="/goods/detail?id={{o.id }}">点击了解详情1
<a href="/goods/details/{{o.id }}">点击了解详情2
{%endfor %}
{%endfor %}
</html>
4.detail.html
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Title
<h1>商品详情
商品名:xxx
商品价格:xxx
产地:xxx
型号:xxx
特点:xxx
<a href="#">返回商品列表
</html>
页面运行效果展示
![](https://img.haomeiwen.com/i22651072/71327fad9eb70288.png)
![](https://img.haomeiwen.com/i22651072/85e412bbb783a7fa.png)
![](https://img.haomeiwen.com/i22651072/884d8ad697786125.png)
![](https://img.haomeiwen.com/i22651072/308ea8cd3d79319b.png)
网友评论