Python - Django - 显示作者列表

在 views.py 中添加展示作者列表的函数

from django.shortcuts import render, redirect, HttpResponse
from app01 import models


# 展示出版社列表
def publisher_list(request):
    pass


# 添加新的出版社
def add_publisher(request):
    pass


# 删除出版社
def del_publisher(request):
    pass


# 编辑出版社
def edit_publisher(request):
    pass


# 展示书籍列表
def book_list(request):
    pass


# 添加书籍
def add_book(request):
    pass


# 删除书籍
def del_book(request):
    pass


# 编辑书籍
def edit_book(request):
    pass


# 作者列表
def author_list(request):
    # 查询所有作者
    all_author = models.Author.objects.all()
    return render(request, "author_list.html", {"author_list": all_author})

然后在 urls.py 中添加显示作者列表的 url 对应关系

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

urlpatterns = [
    # 出版社
    url(r'^publisher_list/', views.publisher_list),
    url(r'^add_publisher/', views.add_publisher),
    url(r'^del_publisher/', views.del_publisher),
    url(r'^edit_publisher/', views.edit_publisher),
    # 书籍
    url(r'^book_list/', views.book_list),
    url(r'^add_book/', views.add_book),
    url(r'^del_book/', views.del_book),
    url(r'^edit_book/', views.edit_book),
    # 作者
    url(r'^author_list/', views.author_list),
]

最后来写 author_list.html




    
    作者列表



作者列表

{% for author in author_list %} {% endfor %}
# id 名字 书籍
{{ forloop.counter }} {{ author.id }} {{ author.name }} {% for book in author.book.all %} {% if forloop.last %} {{ book.title }} {% else %} {{ book.title }} | {% endif %} {% endfor %}

解析:

Python - Django - 显示作者列表_第1张图片

 

运行效果:

Python - Django - 显示作者列表_第2张图片

 

转载于:https://www.cnblogs.com/sch01ar/p/11239353.html

你可能感兴趣的:(Python - Django - 显示作者列表)