【问题标题】:Django attach file with ajaxDjango 使用 ajax 附加文件
【发布时间】:2017-06-12 12:04:19
【问题描述】:

我的模板中有引导模式:

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 发布请求:

$(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 视图中获取文件以将它们附加到电子邮件?

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

或单个文件

myfile = request.FILES['file']

返回“无”

【问题讨论】:

    标签: jquery ajax twitter-bootstrap django-forms django-file-upload


    【解决方案1】:

    回答: 我在 Django 表单的帮助下发布我的数据并从 request.FILES['file']

    获取它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2019-01-11
      • 2012-04-25
      相关资源
      最近更新 更多