Django check if checkbox is selected

后端 未结 3 701
星月不相逢
星月不相逢 2021-01-31 12:09

I\'m currently working on a fairly simple django project and could use some help. Its just a simple database query front end.

Currently I am stuck on refining the searc

3条回答
  •  再見小時候
    2021-01-31 12:45

    In models :

    class Tag:
        published = BooleanField()
        (...)
    

    In the template:

    {% for tag in tags %}
    
    {% endfor %}
    

    Assuming you are sending the form as a POST, the values of the selected checkboxes are in request.POST.getlist('tag').

    For example :

    
    
    
    
    

    Say if 1,4 were checked,

    check_values = request.POST.getlist('tag')
    

    check_values will contain [1,4] (those values that were checked)

提交回复
热议问题