Djangoで複数のチェックボックスから値を取得する方法

Djangoで複数のチェックボックスから値を取得する方法をご紹介します。

条件

  • Django 2.1.7
  • Python 3.7.0

実装方法

テンプレートで以下のようなチェックボックスがあるものとします。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<input type="checkbox" name="checks[]" value="1" />
<input type="checkbox" name="checks[]" value="2" />
<input type="checkbox" name="checks[]" value="3" />
<input type="checkbox" name="checks[]" value="4" />
<input type="checkbox" name="checks[]" value="1" /> <input type="checkbox" name="checks[]" value="2" /> <input type="checkbox" name="checks[]" value="3" /> <input type="checkbox" name="checks[]" value="4" />
<input type="checkbox" name="checks[]" value="1" />
<input type="checkbox" name="checks[]" value="2" />
<input type="checkbox" name="checks[]" value="3" />
<input type="checkbox" name="checks[]" value="4" />

views.pyでは以下のようにしてチェックされた値を取得します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
checks_value = request.POST.getlist('checks[]')
checks_value = request.POST.getlist('checks[]')
checks_value = request.POST.getlist('checks[]')

実装例

テンプレート

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<form id="chk" method="post" name="chk" >
{% csrf_token %}
<input type="checkbox" name="checks[]" value="1" />
<input type="checkbox" name="checks[]" value="2" />
<input type="checkbox" name="checks[]" value="3" />
<input type="checkbox" name="checks[]" value="4" />
<input type="submit" id="button" name="button" value="送信">
</form>
<form id="chk" method="post" name="chk" > {% csrf_token %} <input type="checkbox" name="checks[]" value="1" /> <input type="checkbox" name="checks[]" value="2" /> <input type="checkbox" name="checks[]" value="3" /> <input type="checkbox" name="checks[]" value="4" /> <input type="submit" id="button" name="button" value="送信"> </form>
<form id="chk" method="post" name="chk" >
    {% csrf_token %}
    <input type="checkbox" name="checks[]" value="1" />
    <input type="checkbox" name="checks[]" value="2" />
    <input type="checkbox" name="checks[]" value="3" />
    <input type="checkbox" name="checks[]" value="4" />
    <input type="submit" id="button" name="button" value="送信">
</form>

views.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# views.py抜粋
class IndexView(generic.ListView):
paginate_by = 5
template_name = 'sample/index.html'
model = Post
def post(self, request, *args, **kwargs):
checks_value = request.POST.getlist('checks[]')
logger.debug("checks_value = " + str(checks_value))
# views.py抜粋 class IndexView(generic.ListView): paginate_by = 5 template_name = 'sample/index.html' model = Post def post(self, request, *args, **kwargs): checks_value = request.POST.getlist('checks[]') logger.debug("checks_value = " + str(checks_value))
# views.py抜粋

class IndexView(generic.ListView):

    paginate_by = 5
    template_name = 'sample/index.html'
    model = Post

    def post(self, request, *args, **kwargs):

        checks_value = request.POST.getlist('checks[]')
        logger.debug("checks_value = " + str(checks_value))

実行結果

2と4をチェックした場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
checks_value = ['2', '4']
checks_value = ['2', '4']
checks_value = ['2', '4']

1、3、4をチェックした場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
checks_value = ['1', '3', '4']
checks_value = ['1', '3', '4']
checks_value = ['1', '3', '4']

参考

stackoverflow

https://stackoverflow.com/questions/2417127/how-do-i-get-the-values-of-all-selected-checkboxes-in-a-django-request-post

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です