没有任何异常,但是数据没有存储到数据库

时间:2018-09-10 10:42:18

标签: ajax django

错误:

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

TypeError: 'method' object is not subscriptable

我是ajax的新手,我遇到了checkbox字段的问题,我尝试通过get_list()来访问checkboxes字段,但是不起作用。我应该做些什么更改以发送请求表单ajax并通过view来查看checkbox字段。 我尝试了以下方式

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>

2 个答案:

答案 0 :(得分:0)

您是否知道复选框值仅在选中时才在POST请求中发送(请参见https://www.w3.org/TR/html401/interact/forms.html#checkbox)? 看起来您正在创建模型的实例,仅当您选中复选框时才会保存。 也看看这个Does <input type="checkbox" /> only post data if it's checked?

答案 1 :(得分:0)

我不明白您为什么说您没有收到任何错误,但随后立即发布了您收到的错误。

尽管如此,错误的原因是getgetlist是方法,您需要用括号来调用它们:

sharewith = request.GET.getlist("sharewith")