簡體   English   中英

在django模板中使用spaceless

[英]Using spaceless in django template

我有以下代碼:

    {% for item in profile.jobs.all %}
        {% if not forloop.first %}, {% endif %}{{ item }}
    {% endfor %}

產生如下內容:

"Programmer , Plumber , Philosopher"

我不想在逗號之前使用前導空格,但是我能夠擺脫它的唯一方法是將它壓縮到一行,這降低了可讀性:

{% for item in profile.jobs.all %}{% if not forloop.first %}, {% endif %}{{ item }}{% endfor %}

有沒有更好的方法來處理這個?

{%spaceless%}僅剝離html標記之間的空格。

您可以使用{{value | join:“,”}}

或者我相信這會奏效:

{% for item in profile.jobs.all %}
    {% if not forloop.first %}, {% endif %}
    {{ item }}
{% endfor %}

嘗試{%spaceless%}模板標記: https ://docs.djangoproject.com/en/dev/ref/templates/builtins/?from = olddocs #spaceless

如果你使用類似的東西怎么辦?

{% if profile.jobs.count != 1 %}
    {% for item in profile.jobs.all %}
        {{ item }}{% if not forloop.last %}, {% endif %}
    {% endfor %}
{% else %}
    {{item}}
{% endif %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM