我有一个带有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>更多推荐
发布评论