美文网首页大数据 爬虫Python AI Sql
Django数据信息循环到页面中

Django数据信息循环到页面中

作者: 幼姿沫 | 来源:发表于2020-06-15 13:04 被阅读0次

一   ***   循环列表中信息到页面

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


urls路由配置

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>

    页面运行效果展示

    商品信息系列表 直接显示id信息

    相关文章

    网友评论

      本文标题:Django数据信息循环到页面中

      本文链接:https://www.haomeiwen.com/subject/jrvcxktx.html