Skip to content Skip to sidebar Skip to footer

How Fix To Upload Image From Bootstrap Modal Form In Django

I can not find the information how to load image through django modal form. How to upload a file through a django modal form? my file models.py class Meal(models.Model): restau

Solution 1:

I cant help with the server side code as I don't know django, but the ajax needs to use a FormData object to upload an image file.

$('#modal-meal').on('submit','.create-form' , function(){
    var form = $(this);
    var fd = new FormData(this);
    $.ajax({
        url: form.attr('data-url'),
        data: fd,
        type: form.attr('method'),
        dataType: 'json',
        processData: false,
        contentType: false,
        success: function(data){
            if(data.form_is_valid){
                $('#meal-table tbody').html(data.meal_list);
            $('#modal-meal').modal('hide');
        } else {
            $('#modal-meal .modal-content').html(data.html_form)
        }
    }
    })
    return false;
})

Post a Comment for "How Fix To Upload Image From Bootstrap Modal Form In Django"