Django 附加文件 ajax

Django attach file with ajax

我的模板中有 bootstrap 模式:

div class="modal fade" id="offerModalForm" tabindex="-1" role="dialog"
             aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <!-- Modal Header -->
                    <!-- Modal Body -->
                    <div class="modal-body">
                        <form role="form" action="ordercomplete" method="post" enctype="multipart/form-data">
                            {% csrf_token %}

                            <div class="modal-header">
                                <button type="button" class="close"
                                        data-dismiss="modal">
                                    <span aria-hidden="true">&times;</span>
                                    <span class="sr-only">Close</span>
                                </button>
                                <h4 class="modal-title" id="myModalLabel">
                                    Оформить заказ
                                </h4>
                            </div>
                            <div class="form-group">
                                <label for="offerInputPhone">Номер телефона</label>
                                <input type="text" class="form-control bfh-phone" id="offerInputPhone"
                                       name="order_phone"
                                       placeholder="Введите номер телефона" data-country="UA">

                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name="callback" id="offerCheckbox"/> Обратный звонок </label>

                            </div>
                            <div class="form-group">
                                <label for="offerInputEmail">Электронная почта</label>

                                <input type="text" class="form-control"
                                       name="email" id="offerInputEmail" placeholder="Введите адрес электронной почты"/>
                            </div>


                            <div class="form-group">
                                <label for="comment">Детали заказа</label>
                                <textarea class="form-control" rows="7" id="offerDetails" name="description"
                                          placeholder="Опишите более детально что необходимо сделать, сроки, стоимость"></textarea>
                            </div>
                            <div class="alert alert-danger" style="display:none;" id="emptyvalueAlert" role="alert">
                                Электронная почта или телефон должны быть заполнены.
                            </div>
                            <div class="alert alert-danger" style="display:none;" id="incorrectPhoneNumber"
                                 role="alert">
                                Некорректно указан номер телефона, возможно Вы пропустили цифру.
                            </div>
                            <div class="alert alert-danger" style="display:none;" id="incorrectEmailNumber"
                                 role="alert">
                                Некорректно указана электронная почта, возможно Вы пропустили какой-то символ.
                            </div>
                            <input type="file" name="uploadfile" id="uploadfile"/>

                            <div class="modal-footer">
                                <button type="submit" id="offerSubmitBtn" class="btn btn-primary" type="submit">
                                    Оформить заказ
                                </button>
                                <button type="button" class="btn btn-primary"
                                        data-dismiss="modal">
                                    Закрыть
                                </button>
                            </div>

                        </form>


                    </div>

                    <!-- Modal Footer -->

                </div>
            </div>

我还有 ajax post 要求:

$(document).ready(function () {
    $('#offerSubmitBtn').on('click', function (e) {
        e.preventDefault(); // prevent the default form submit action
        var data = new FormData();
        jQuery.each(jQuery('#file')[0].files, function (i, file) {
            data.append('file-' + i, file);
        });

        $.ajax({
            type: "POST", //or POST
            url: "make_order/",
            enctype: "multipart/form-data",
            data: {the_post: $('#offerInputPhone').val(), file_input: data},
            success: function (data) {
                window.location.href = 'ordercomplete.html'
            }
        });
    })
});

如何在 Django 视图中获取文件并将其附加到电子邮件中?

文件 = request.FILES.getlist('file')

或单个文件

我的文件 = request.FILES['file']

returns "None"

回答: 我 post 我的数据借助 Django 表单并从 request.FILES['file']

获取