展示出版社:写上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:




Title












{% for publisher in all_publishers %}





{% endfor %}

序号 id 出版社名称
{{ forloop.counter }} {{ publisher.pk }} {{ publisher.name }}




页面展示效果:

 

 

 

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