Ajax 没有向 Django 视图发送数据

Ajax is not sending data to django views

我只写了需要的东西。我是新手所以请忽略问题质量:).

我的Html---

<div class="form-group row pt-3 d-flex justify-content-center">
            <div class="col-sm-10 d-flex justify-content-center">
                <button id="submit_pic" style="width: 70%;" type=""
                    class="btn btn-primary">Combine</button>
                <script>

                    $("#submit_pic").submit(function (e) {
                        e.preventDefault();
                        var csrfToken = $("input[name='csrfmiddlewaretoken']");
                        
                        $.ajax({
                            url: "{% url 'make_combine' %}",
                                //url: "/make_combine",
                            type: "POST", //-------here is problem-------
                            dataType: "json",
                            cache: true,
                            headers: {'csrfmiddlewaretoken': csrfToken.val()},
                            data: {"for_combine": response_send },
        
                            //"success": function (result) {
                            //    console.log(data);
                            //},
                        });
                        return false;
                    });

                    
                </script>
            </div>
         </div>

django views.py --

import json
def make_combine(request):
      combine_data7 = request.POST.get("for_combine")

我已经在互联网和 Whosebug 上尝试了很多可用的技巧,但仍然得到这个 error.It 告诉 combine_data7 是 NoneType ----

    AttributeError at /make_combine
    'NoneType' object has no attribute 'split'

谢谢Mam/Sir。

submit_pic 应该在 form id 内,而不是在 button id 内。试试下面的代码,它会 100% 有效。此外,在表单内提供 enctype="multipart/form-data" 属性是一种很好的做法。

<div class="form-group row pt-3 d-flex justify-content-center">
    <form id='submit_pic' enctype="multipart/form-data">
        <div class="col-sm-10 d-flex justify-content-center">
            <button style="width: 70%;" type="" class="btn btn-primary">Combine</button>
        </div>
    </form>
</div>
<script>
    $("#submit_pic").on('submit', function (event) {
        event.preventDefault();
        var csrfToken = $("input[name=csrfmiddlewaretoken]").val();
        console.log(`Error ye raha === ${response_send}`)
        $.ajax({
            url: "{% url 'make_combine' %}",
            //url: "/make_combine",
            type: "post", //-------here is problem-------
            dataType: "json",
            cache: true,
            headers: {
                'csrfmiddlewaretoken': csrfToken
            },
            data: {
                "for_combine": response_send
            },

            //"success": function (result) {
            //    console.log(data);
            //},
        });
        return false;
    });
</script>