Использование переменной в теге шаблона Django yesno - PullRequest
0 голосов
/ 28 октября 2019

Это может быть довольно просто. Тем не менее, позвольте мне спросить. В теге встроенного шаблона Django yesno синтаксис задается как {{ value|yesno:"yeah,no,maybe" }}.

Однако я хочу вывести значение переменной, если оно имеет значение true, что-то вроде: {{ request.user.profile.mobile_number|yesno:$variable1, 'string' }}.

Я пробовал это {{ request.user.profile.mobile_number|yesno:"{{ request.user.profile.mobile_number }},No Contact" }}, но постоянно получаю сообщение об ошибке: Could not parse the remainder: ':"{{ request.user.profile.mobile_number' from 'request.user.profile.mobile_number|yesno:"{{ request.user.profile.mobile_number'

1 Ответ

2 голосов
/ 28 октября 2019

Попробуйте:

{% with yesno_args=request.user.profile.mobile_number|add:", No Contact" %}
{{ request.user.profile.mobile_number|yesno:yesno_args }}
{% endwith %}

В качестве альтернативы, которая может быть более понятной:

{% if request.user.profile.mobile_number is not None and request.user.profile.mobile_number != '' %}
<span>{{ request.user.profile.mobile_number }}</span>
{% else %}
<span>No Contact</span>
{% endif %}

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...