Django新手ManyToManyField模板问题(Django Newbie ManyToManyField Template Question)

编程入门 行业动态 更新时间:2024-10-24 07:28:37
Django新手ManyToManyField模板问题(Django Newbie ManyToManyField Template Question)

我有一个带有ManyToManyField的Django模型,我试图在模板中的逗号分隔列表中迭代该字段的内容。 我收到了一些意想不到的结果。

{% for painting in paintings_list %} <p>{% for item in painting.style.all %} {{ item.style|join:', ' }} {% endfor %}</p> {% endfor %}

内容正在显示,因为它们存在于数据库中,但它们以意想不到的方式显示...即。 代替:

文艺复兴时期,巴洛克式,表现主义

我越来越:

R,E,N,A,I,S,S,A,N,C,E ,, B,A,R,O,Q,U,E ,, E,X,P,R,E,S, S,I,O,N,I,S,T

任何想法我做错了什么? 本来以为连接模板过滤器就是用于这种类型的场景,但也许正确的做法是创建模型的自定义方法......

I have a Django model with a ManyToManyField and I'm trying to iterate the contents of that field in a comma-delimited list in my template. I'm getting some unexpected results.

{% for painting in paintings_list %} <p>{% for item in painting.style.all %} {{ item.style|join:', ' }} {% endfor %}</p> {% endfor %}

The contents are being displayed as they exist in the database, but they're displayed in an unanticipated way...ie. instead of:

Renaissance, Baroque, Expressionist

I'm getting:

R,e,n,a,i,s,s,a,n,c,e,,B,a,r,o,q,u,e,,E,x,p,r,e,s,s,i,o,n,i,s,t

Any idea what I'm doing wrong? Would have thought the join template filter was for exactly this type of scenario, but perhaps the proper way to do this would be to create a custom method of the model...

最满意答案

伊格纳西奥瓦斯奎兹 - 艾布拉姆斯是正确的(像往常一样)。 解决你的问题可能在于forloop变量。

<p>{% for item in painting.style.all %} {{item.style}} {% if not forloop.last %}, {% endif %} {% endfor %}</p>

Ignacio Vasquez-Abrams is correct (as usual). A solution to your problem might lie in the forloop variables.

<p>{% for item in painting.style.all %} {{item.style}} {% if not forloop.last %}, {% endif %} {% endfor %}</p>

更多推荐

本文发布于:2023-07-05 06:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   新手   ManyToManyField   Django   Template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!