django 多对多字段 前端html渲染时

django 多对多字段 前端html渲染时,第1张

book表中有ManytoMany字段, views.py 中 all_book_list = Book.objects.all()即可

{% for book in all_book_list %} #先循环Book表,获得所有Book对像
                   
                        {{ book.id  }}
                        {{ book.title }}
                        {{ book.price }}
                       
                            {% for book_author in book.authors.all %} #重点!!!!再循环Book表的多对多对像 author 获的对应的 book_author对像
                                {{ book_author }}
                            {% endfor %}
                        #要注意将循环放在需要循环的标签内
                        {{ book.publisher }}
                        {{ book.publication_date|date:'Y-m-d'}}
                       
                   
                {% endfor %}

类似下图:

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/905109.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-15
下一篇2022-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存