all_book.html
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查看所有书籍title>
head>
<body>
<table border="1">
<tr>
<th>idth>
<th>titleth>
<th>pubth>
<th>priceth>
<th>infoth>
<th>market_priceth>
<th>opth>
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),
]
没有报错,但网页显示不出数据库的数据,前端页面也只是简单地显示{{ book.id }},没有显示参数值,请各位帮帮忙!
view函数里定义的是a11_book,template里面调用的是all_book啊。。。
把template里面所有all_book改为a11_book应该就可以了
如果有用请采纳 谢谢
你是直接打开了html,打开下面网址看看
http://127.0.0.1:8000/all_book