展示出版社:写上URL地址对应函数、函数当中查询出所有的出版社、对象交给模板、循环对象拿出每条数据展示

URL:

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^publisher/', views.publisher),
]

views:
from django.shortcuts import render
from app01 import models

# Create your views here.

def publisher(request):
#查询出所有出版社的信息:
all_publishers = models.Publisher.objects.all()
#返回一个页面:第一个参数request、HTML页面、字典形式:第一个是字典的key字符串形式、第二个是从数据库的中name
return render(request,"publisher.html",{"all_publishers":all_publishers})

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<table border="1">
<thead>
<tr>
<th>序号</th>
<th>id</th>
<th>出版社名称</th>
</tr>
</thead>
<tbody>
{% for publisher in all_publishers %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ publisher.pk }}</td>
<td>{{ publisher.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

页面展示效果:
 

猜你喜欢

转载自www.cnblogs.com/zhang-da/p/12037063.html
今日推荐