我正在尝试为Django中的块动态创建div类标记值。我有注释树,并决定为注释块创建最大值限制(仅在模板中不在db中)。所以我创建了下一个模板。它工作正常,但有太大的行,我不能插入任何空格和新的行符号,因为他们打破模板或保持在页面html源。这是在评论之后。
{% extends 'myblog/base.html' %}
{% load bleach_tags %}
{% block title %}{{ article.name|bleach }}{% endblock %}
{% block content %}
<a href="{% url 'myblog:article_detail' article.id %}"><h2>{{ article.name|bleach }}</h2></a>
<div class = "post_body_detail">
{{ article.text|bleach }}
</div>
<div class = "comments">
{% for comment in comment_list %}
<li>
{# (next line is too big) div class comment level can not be bigger max value for marking purposes #}
<div class = "comment{% if comment.level <= comment.MAX_COMMENT_DIV_BLOCK_DEEP %}{{comment.level}}{% else %}comment.MAX_COMMENT_DIV_BLOCK_DEEP{% endif %}">
{{ comment.text|bleach}}
</div>
</li>
{% empty %}
<li>No comments yet.</li>
{% endfor %}
</div>
{% endblock %}
如何分割此行以便于阅读(格式化)?
答案 0 :(得分:0)
试试这个:
{% with max_deep=comment.MAX_COMMENT_DIV_BLOCK_DEEP %}
<div
{% if comment.level <= max_deep %}
class="comment{{ comment.level }}"
{% else %}
class="comment{{ max_deep }}"
{% endif %}
>
{{ comment.text|bleach}}
</div>
{% endwith %}