抹茶芋泥 2022-08-12 07:29 采纳率: 60%
浏览 53
已结题

前端写的html通过django接收不到后台mysql的数据

前端写的html通过django接收不到后台mysql的数据

相关代码

all_book.html


 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查看所有书籍</title>
</head>
<body>

<table border="1">
    <tr>
        <th>id</th>
        <th>title</th>
        <th>pub</th>
        <th>price</th>
        <th>info</th>
        <th>market_price</th>
        <th>op</th>
    </tr>
    {%  for book in all_book %}
    <tr>
        <td>{{ book.id }}</td>
         <td>{{ book.title }}</td>
         <td>{{ book.pub }}</td>
        <td>{{ book.info }}</td>
         <td>{{ book.price }}</td>
         <td>{{ book.market_price }}</td>
         <td>
             <a href="/bookstore/update_book/{{ book.id }}">更新</a>
             <a href="">删除</a>
         </td>
    </tr>
    {% endfor %}

</table>
</body>
</html>

views.py

def all_book(request):

    a11_book = Book.objects.all()
    # a11_book = a11_book.encode('utf-8').decode('utf-8')
    # a11_book = a11_book.encode(errors='replace')
    return render(request, 'bookstore/all_book.html', locals())

主路由urls.py

from django.contrib import admin
from django.urls import path, re_path, include
from .import views

urlpatterns = [
     # 数据库查询视图,主路由配分发
    path('bookstore/', include('bookstore.urls'))
]

分支路由bookstore/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('all_book', views.all_book),
]

img

img

没有报错,但网页显示不出数据库的数据,前端页面也只是简单地显示{{ book.id }},没有显示参数值,请各位帮帮忙!

  • 写回答

2条回答 默认 最新

  • ポテトエルフ 2022-08-12 15:27
    关注

    view函数里定义的是a11_book,template里面调用的是all_book啊。。。
    把template里面所有all_book改为a11_book应该就可以了
    如果有用请采纳 谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月22日
  • 已采纳回答 8月14日
  • 创建了问题 8月12日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?