1

I have the below List in Python.

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

and need to pass this list values into a Check box with Jinja Template.

Can someone help on this with HTML code embedded with Jinja template ?

Expected Output:-

enter image description here

HTML Need to converted with JINJA Template.

<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>

2
  • in what prior do u want the checkboxes to be checked? on what conditions? do u do it manually? Commented Oct 25, 2021 at 13:56
  • I want the checkboxes to be checked and un-checked manually by user ... Could you help me with the that as well . Currently I couldn't check/un-check the checkboxes.. Commented Oct 27, 2021 at 6:43

1 Answer 1

3

Try this code

This code will have input tags with values like approved, un-approved, deacitvate instead of app, unapp, deac. Is that okay for you?

And also its better to put the input tag inside the label tag, because when you click the word beside the checkbox, it'll toggle the checkbox (and thats why labels are mostly used for)

As W3schools says:

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 %}

And also pass the list_val list and its length in seperate keyword arguments on the render_template function like

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))

Tell me if its not working...

Sign up to request clarification or add additional context in comments.

7 Comments

yes it's working now Thanks a lot.... BUT , I couldn't check the checkbox values !!! Can help ??
@lazarcognos i don't get it. do u want to get the value of the checkbox? and also want to use python right?
I couldn't check/un-check the checkboxes.. Could you help me with the that as well
@lazarcognos but i can check/un-check the checkboxes actually, do u mean like toggle them using code?
Yes exactly. Currently, i couldn't able to check/un-check them. Sample working code here will be much helpful
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.