0

error:

sharewith=request.GET.get["sharewith"]

TypeError: 'method' object is not subscriptable

I am new to ajax,getting issue with checkbox field, i tried get_list() of to access checkboxes field but not working.what change should i make to send request form ajax and access through view for checkbox fields. I tried in the following way

javascript:

function submitContactForm() {
var token = '{{csrf_token}}';
var name = $('#inputName').val();
var sharewith = $("#sharewith").val()
if (name.trim() == '') {
    alert('Please enter your name.');
    $('#inputName').focus();
    return false;
}else{
    $.ajax({
        headers: { "X-CSRFToken": token },
        type:'POST',
        url:'sharing',
        dataType:"json",
        traditional: true,
        data:'contactFrmSubmit=1&name='+name+'&sharewith'+sharewith,
        beforeSend: function () {
            $('.submitBtn').attr("disabled","disabled");
            $('.modal-body').css('opacity', '.5');
        },
        success:function(msg) {
            if (msg == 'ok') {
                $('#inputName').val('');
                $('.statusMsg').html('<span style="color:green;">sucessfully saved</p>');
            } else {
                $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
            }
            $('.submitBtn').removeAttr("disabled");
            $('.modal-body').css('opacity', '');
        }

    });
}

}

views.py:

  def test(request):
if request.method == "POST" and request.is_ajax() :
    if "name" in request.POST:
       name=request.POST["name"]
       sharewith=request.POST.getlist["sharewith"]
       instance=Test.objects.create(name=name)
       for user in sharewith:
          instance.add(user)
          instance.save()
       return HttpResponse(json.dumps({'msg': "ok"}), content_type="application/json")
 else:
        return render(request,template_name='registration/basetest.html')

form:

   <div class="modal-body">
            <p class="statusMsg"></p>
            <form role="form">{% csrf_token %}
                <div class="form-group">
                    <label for="inputName">knowledge category</label>
                    <input type="text" class="form-control" id="inputName" placeholder="Enter your name"/>
                </div>
                <div class="form-check" id="sharewith">
                    <label for="sharewith">Share with</label></br>
                 {% for sharewith in sharewithonly %}
                  <input class="form-check-input position-static" type="checkbox"  value="{{ sharewith.id }}">
                     <label>{{ sharewith.email }}</label></br>
                  {% endfor%}
                </div>

            </form>
        </div>
joe
  • 17
  • 4

2 Answers2

0

Are you aware that the checkbox value is only sent in the POST request when checked (see https://www.w3.org/TR/html401/interact/forms.html#checkbox)? It looks like you are creating an instance of your model that is only saved when you have the checkbox checked. Have a look on this too Does <input type="checkbox" /> only post data if it's checked?

lexotero
  • 66
  • 4
0

I don't understand why you said you weren't getting any error, but then immediately posted the error that you got.

Nevertheless, the reason for the error is that get and getlist are methods, you need to call them with parentheses:

sharewith = request.GET.getlist("sharewith")
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895