django 学习-11 Django模型数据模板呈现

1、for author in Author.objects.all():

    for book in author.book_set.all():

      print   book

2、vim blog/views.py

from blog.models  import Author,Book

from  django.shortcuts   import  render_to_response

    def show_author(req):

        authors = Author.objects.all()

        return render_to_response('show_author.html',{'authors':authors})    先写好视图函数

vim blog/templates/show_author.html

{{authors}}

3、python   manage.py   runserver

然后127.0.0.1:8000/blog/show_author.html

4、vim show_author.html


{%for author in authors%}
<li>{{author}}</li>
{%endfor%}
用for 循环做个遍历
      

原文地址:https://www.cnblogs.com/Icanflyssj/p/5131678.html