ошибка:
sharewith=request.GET.get["sharewith"]
TypeError: 'method' object is not subscriptable
Я новичок в ajax, получаю проблему с полем флажка, я попытался get_list () для доступа к полю флажков, но не работает. Какое изменение я должен сделать, чтобы отправить форму запроса ajax и получить доступ через поля для просмотра флажка.
Я пробовал следующим образом
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')
Форма:
<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>