【问题标题】:How to make a checkbox checked in Jinja2如何在 Jinja2 中选中复选框
【发布时间】:2020-12-21 10:05:13
【问题描述】:

我正在学习烧瓶编程,但不知道如何检查无线电输入,这是我正在使用的 html 模板:

<form method = "POST" action="/proxy_settings">
    <input type="radio" name="proxy_mode" value = '0'>Auto
    <br>
    <input type="radio" name="proxy_mode" value = '1'>Manual
    <br>
    <br>
    <section>
        <table border="1">
            <tr>
                <td>Description</td>
                <td>delay</td>
                <td>select</td>
            </tr>
            {% for node_name, node_delay in node_list.items() %}
            <tr>
                <td>{{node_name}}</td>
                <td>{{node_delay}}</td>
                <td><input type="radio" name="proxy_node"></td>
            </tr>
            {% endfor %}
        </table>
    </section>
    <br>
    <section>
        <button type="submit">CONFIRM</button>
    </section>
</form>

我正在像这样在烧瓶中渲染这个模板:

return render_template("base.html", node_number = ret["node_number"], proxy_mode = ret1["proxy_mode"], proxy_node = ret2["proxy_node"], node_list=ret3) 

我的问题是:

  1. 如何根据变量proxy_mode的值检查proxy_mode单选输入?
  2. 如何根据变量proxy_node的值检查proxy_node单选输入?例如,如果 proxy_node 等于 2,则将检查表第 2 行中的无线电输入。
  3. 如何为单选输入proxy_node动态分配value属性?

问题1我试过下面的方法,但是不行。

<input type="radio" name="proxy_mode" value = '0' {% if proxy_mode == 0 %} checked=true {% endif %}>Auto
<br>
<input type="radio" name="proxy_mode" value = '1' {% if proxy_mode == 1 %} checked=true {% endif %}>Manual

提前致谢!

【问题讨论】:

    标签: python html flask jinja2


    【解决方案1】:

    这应该是最简单的方法了。

    <input type="radio" name="proxy_mode" value="0" {{ "checked" if proxy_mode == 0 }}>Auto
    <br>
    <input type="radio" name="proxy_mode" value="1" {{ "checked" if proxy_mode == 1 }}>Manual
    

    【讨论】:

      【解决方案2】:

      在阅读了 Jinja2 Template Designer Documentationanother stackoverflow question 之后,我能够为我的问题想出一个解决方案: 使用 Jinja 语句来控制整个 html 标签而不是这个标签的属性。所以,修改后的表格是这样的:

              <form method = "POST" action="/proxy_settings">
                  {% if proxy_mode == '0' %}
                  <input type="radio" name="proxy_mode" value = '0' checked=true>Auto
                  {% else %}
                  <input type="radio" name="proxy_mode" value = '0'>Auto
                  {% endif %}
                  <br>
                  {% if proxy_mode == '1' %}
                  <input type="radio" name="proxy_mode" value = '1' checked=true>Manual
                  {% else %}
                  <input type="radio" name="proxy_mode" value = '1'>Manual
                  {% endif %}
                  <br>
                  <br>
                  <section>
                      <table border="1">
                          <tr>
                              <td>Description</td>
                              <td>delay</td>
                              <td>select</td>
                          </tr>
                          {% for node_name, node_delay in node_list.items() %}
                          <tr>
                              <td>{{node_name}}</td>
                              <td>{{node_delay}}</td>
                              {% if loop.index0 == proxy_node|int %}
                              <td><input type="radio" name="proxy_node" value={{loop.index0}} checked=true></td>
                              {% else %}
                              <td><input type="radio" name="proxy_node" value={{loop.index0}}></td>
                              {% endif %}
                          </tr>
                          {% endfor %}
                      </table>
                  </section>
                  <br>
                  <section>
                      <button type="submit">CONFIRM</button>
                  </section>
              </form>
      

      希望此答案对查看此问题的人有所帮助。谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        • 2012-01-10
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        • 1970-01-01
        相关资源
        最近更新 更多