L_Augety 2022-08-28 01:31 采纳率: 66.7%
浏览 30
已结题

django 数据库一对多查询拿到json数据后,怎么渲染到前端html页面?

django中 已经一对多查询完成返回json数据了,如何让前端html拿到json数据并渲染到前端html里面,进行for循环,

img

img

  • 写回答

1条回答 默认 最新

  • honestman_ 2022-08-28 09:18
    关注

    传入context字典,就可以让前端通过{{}}或者{% %}的形式进行访问了

       context = {
            'customer': customer,
            'c_count': c_count,
            'order': order,
            'o_count': o_count,
        }
        return render(request, 'aoapp/home.html',context)
    
    
      <h3>Total Customer: {{ c_count }}</h3>
      <table class="table">
                        <thead class="table-dark">
                        <tr>
                            <th scope="col">C_ID</th>
                            <th scope="col">Name</th>
                            <th scope="col">Phone</th>
                            <th scope="col">Email</th>
                            <th scope="col">Time</th>
                            <th scope="col">View</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for i in customer reversed %}
                            <tr>
                                <th scope="row">{{ i.id }}</th>
                                <td>{{ i.name }}</td>
                                <td>{{ i.phone }}</td>
                                <td>{{ i.email }}</td>
                                <td>{{ i.time_cteated }}</td>
                            </tr>
                        {% endfor %}
    
                        </tbody>
                    </table>
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

    报告相同问题?

    问题事件

    • 系统已结题 11月11日
    • 已采纳回答 11月3日
    • 创建了问题 8月28日

    悬赏问题

    • ¥15 我用C语言easyx图形库绘制了一个3d游戏方框透视,但进入游戏时候鼠标准星对准方框边缘 鼠标光标就会弹出来这是啥情况怎样让光标对准绘制的方框点击鼠标不弹出光标好烦这样
    • ¥20 用Power Query整合的问题
    • ¥20 基于python进行多背包问题的多值编码
    • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
    • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
    • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
    • ¥15 C语言:数据子序列基础版
    • ¥20 powerbulider 导入excel文件,显示不完整
    • ¥15 用keil调试程序保证结果进行led相关闪烁
    • ¥15 paddle训练自己的数据loss降不下去