Измените имя столбца запроса на переменную в шаблоне Django - PullRequest
0 голосов
/ 14 июля 2020

Я использую структуру django для выполнения своей задачи, я хочу оптимизировать свой код в файле шаблона. Вот мой кодовый удар.

            {% ifequal name 'jack' %}
              {% ifequal item.jack 'lower' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
              {% endifequal %}
              {% ifequal item.jack 'higher' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
              {% endifequal %}
              {% ifequal item.jack 'unknown' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
              {% endifequal %}
            {% endifequal %}


            {% ifequal name 'du01' %}
              {% ifequal item.du01 'lower' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
              {% endifequal %}
              {% ifequal item.du01 'higher' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
              {% endifequal %}
              {% ifequal item.du01 'unknown' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
              {% endifequal %}
            {% endifequal %}

Я хочу, чтобы кнопка зависела от разных name. name теперь является переменной. Однако он отображается на имя столбца в таблице. Можно ли сделать что-то вроде удара, чтобы очистить мой код?

              {% ifequal item.{{name}} 'lower' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
              {% endifequal %}
              {% ifequal item.{{name}} 'higher' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
              {% endifequal %}
              {% ifequal item.{{name}} 'unknown' %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
              {% else %}
                <button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
              {% endifequal %}

1 Ответ

0 голосов
/ 14 июля 2020

Вы не можете этого сделать. Вместо этого вы можете изменить объект элемента на словарь.

Затем вы можете найти значение словаря в шаблоне

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