10

index.html

<td>{% if place or other_place or place_description%}{{ place}} {{ other_place}} {{place_description}}</td>

これは、テンプレート内のすべてのデータを表示しています。文字列の長さが 80 を超える場合は、文字列を切り捨てたいです。

条件は次のとおりです。 1.place 変数が 80 文字を超える場合は、それらを切り捨て、other_place や place_description などの他の 2 つの変数を表示する必要はありません。

2.place 変数と other_place 変数が 80 文字を超える場合、place_variable から切り捨てるべきで、place_description 変数を表示する必要はありません。

3.3 つすべてが自分のもので、80 番目の文字が place_description から作成されている場合、それらから切り捨てる必要があります。

すべてのフィールドは必須ではないため、どのフィールドが表示されても、80 文字しか表示されません。

これを行うには助けが必要です。

ありがとう

4

3 に答える 3

17

django 1.4 より前のバージョンではスライスを使用できます。

{% if place or other_place or place_description%}
    {% with place|add:other_place|add:place_description as pl %}
        {% if pl|length > 80 %}
            {{pl|slice:80}}...
        {% else  %}
            {{pl }}
        {% endif %}
    {% endwith %}
{% endif %}

django 1.4 以降を使用している場合は、

あなたはただ使うことができますtruncatechars

{% if place or other_place or place_description%}
    {% with place|add:other_place|add:place_description as pl %}
       {{pl|truncatechars:80}}
    {% endwith %}
{% endif %}
于 2013-07-11T14:38:29.220 に答える