Djagno 使用locals()

Django使用locals()函数

locals()函数会以字典类型返回当前位置的全部局部变量

在 views.py中添加

#展示
class Goods_list(View):
    def get(self, request):
        goods = Goods.objects.all()
        return render(request, 'goodslist.html', locals())

在html中写入

<table border="1">
                <tr>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>图片</th>
                    <th>操作</th>
                </tr>
                {% for i in goods %}
                <tr>
                    <td>{{i.name}}</td>
                    <td>{{i.price}}</td>
                    <td><img src="{{i.img}}" width="150" height="100"></td>
                    <td> <a href="javascript:;" class="delete">删除</a></td>

                </tr>
                {% endfor %}


</table>

结果

posted @ 2022-12-22 10:35  乐乐乐乐乐乐樂  阅读(56)  评论(0)    收藏  举报
jQuery火箭图标返回顶部代码