0

I am using an html table to display some data with checkboxes as the first column, so the user can select multiple rows and delete them simultaneously. The html code to create the checkboxes is this:

<td>
    <input type="checkbox" name="checkbox{{row .index}}"
        value={{row .index}}>
    {{row .like}}
</td>

I'm trying to get a list of all the indexes of the selected rows, so I can pass them on to a function which will delete the rows from the database. Currently I'm just using these two lines to get the list, but I get an empty list every time:

    print(request.form.getlist('checkbox'))
    return 'Done'

Do I have a stupid mistake somewhere? PS. I have almost the entire html doc on one form, because it's a quite simple page and just makes things easier.

2 Answers 2

2

The problem is with the names of your checkbox options. They are all different as you are using a variable. Try using this name:

name="checkbox"

in this line of your code:

<input type="checkbox" name="checkbox{{row .index}}"
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @IoaTzimas, I got somewhere, but not entirely there... Now this is printed: ['<built-in', '<built-in'] depending on how many checkboxes were selected. Any idea why?
Are you sure that you pass the correct values inside the checkbox in your html file?
That's what I'm trying to figure out. I saw in another thread someone said to add the value={{row .index}}> part, but I can't seem to find anything about that. In pycharm, I got a warning saying "attribute .index is not allowed here" at that specific part, but I ignored it because I don't know what it means or why not. I just trusted the person who said I should use the above line.
Check how you can pass values to html with Flask, you will find plenty of info
What do you mean? Like why would I send data from python to html in this case?
|
0

I finally got it to work. IoaTzimas answer was one of the things, but the ['<built-in'] problem was fixed by changing value={{row .index}} to value={{data.index(row)}}. It's quite obvious actually, because the index of row is searched for in the list 'data'. Small mistake. Thanks for the help!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.