在 Python Flask 中创建复选框 - Jinja 模板

Create CheckBox in Python Flask - Jinja Template

我在 Python 中有以下列表。

list_val = ['APPROVED','UN-APPROVED','DEACTIVATE']

并且需要将此列表值传递到带有 Jinja 模板的复选框中。

有人可以用 HTML 嵌入 Jinja 模板的代码来帮助解决这个问题吗?

预期输出:-

HTML需要用JINJA模板转换。

<input type="checkbox" id="val1" name="val1" value="app">
<label for="val1"> APPROVED</label><br>
<input type="checkbox" id="val2" name="val2" value="unapp">
<label for="val2"> UN-APPROVED</label><br>
<input type="checkbox" id="val3" name="val3" value="deac">
<label for="val3"> DEACTIVATE</label>

试试这个代码

此代码将包含具有 approvedun-approveddeacitvate 等值的输入标签,而不是 appunappdeac .你觉得合适吗?

最好将 input 标签放在 label 标签内,因为当您单击复选框旁边的单词时,它会切换复选框(这就是标签主要用于的原因)

正如 W3schools 所说:

Proper use of labels with the elements above will benefit:

  • Screen reader users (will read out loud the label, when the user is focused on the element)
  • Users who have difficulty clicking on very small regions (such as checkboxes) - because when a user clicks the text within the <label> element, it toggles the input (this increases the hit area).
  • Tip: The for attribute of <label> must be equal to the id attribute of the related element to bind them together. A label can also be bound to an element by placing the element inside the <label> element.
{% for i in range(list_val_len) %}
    <label for="val{{ i+1 }}" name="val{{ i+1 }}">
        <input type="checkbox" id="val{{ i+1 }}" name="val{{ i+1 }}" value="{{ list_val[i].lower() }}">
        {{ list_val[i] }}
    </label><br>
{% endfor %}

并且还在 render_template 函数的单独关键字参数中传递 list_val 列表及其长度,例如

list_val = ['APPROVED','UN-APPROVED','DEACTIVATE']

@app.route('whatever_route_in_here')
def whatever_name_your_function_has():
    ...
    ...
    render_template('html_file_name.html', list_val=list_val, list_val_len=len(list_val))

如果它不起作用请告诉我...