首页 > 解决方案 > 模板中没有来自 jquery 的值到我的视图函数

问题描述

我正在使用 django,我从复选框中获取所有 id,现在我想将它们传递给我的视图函数,但我不知道如何。我试过 request.GET.getlist('vals') 但没有成功。有什么建议么?

事件.html:

<script>
    $(document).ready(function(){
        $("button").click(function(){
        type: 'POST';
            var vals = [];
            $.each($("input[name='checkb']:checked"),function(vals){
                vals.push($(this).attr('id'));
            });
            alert("values: " + vals.join(", "));
        });
    });
</script>

    <td><a href="eprint"><button><i class="bi bi-sim"></i></button></a></th>

    {% for l in object_list %}
      <tr>
    <td>
      <form>
        <label><input type="checkbox" id={{l.pk}} name="checkb"></label>
      <form>
...

网址.py:

   path('eprint',views.eprint,name='eprint'),

视图.py:

def eprint(request):

    print('eprint')
    v=request.GET.getlist(vals)

标签: pythonjquerydjango

解决方案


好的,我终于解决了这个问题。我正在使用 AJAX 将 id 发送到我的视图:

模板中的java脚本:

<script>
    $(document).ready(function() {
        $("button").click(function() {
          var vals = [];
      $.each($("input[name='checkb']:checked"), function() {
            vals.push($(this).attr('id'));
      });
      console.log('cons2:',vals)
      $.get('eprint/ticked/',{marked: vals}) 
      console.log('after')
        });
    });
</script>

视图.py:

def eprint(request):

    print('eprint')
    print(request.GET)

推荐阅读