1

I am using django with VueJS. The data is updating properly in my database.

I need to accomplish two things:

  1. Post the correct content to the field image_file.
  2. Get the downloaded image file pasted onto the servers folder which is media/shop/images

My attempted code is as below:

models.py

...
image_file = models.ImageField(upload_to='shop/images/', blank=True, null=True)

urls.py

...
urlpatterns += [
   url(r'^Post-Items-Axios$', myviews.Post_Items_Axios, name='Post-Items-Axios'),
]

views.py

@api_view(['GET', 'POST', 'PUT', 'DELETE'])
def Post_Items_Axios(request):
    if request.method == 'POST':
        data_itemfullhd = request.data['Item Name']
        data_image_file = request.data['Item Image File']

        td_items, created = Md_Items.objects.get_or_create(
            itemfullhd = data_itemfullhd)

        td_items.imagefl = data_imagefl
        td_items.image_file = data_image_file
        td_items.save()

    data = { 'data_itemfullhd': data_itemfullhd }
    return Response(data)

bm_home.html

<template id="product-edit">
    <div>
    <h2>Product details</h2>
    <form method="post" enctype="multipart/form-data">{% csrf_token %}
        <div class="form-group">
            <label for="edit-name">Item Name</label>
            <input class="form-control" id="edit-name" v-model="product.itemfullhd" required/>
        </div>

        <!-- Upload single Image files -->
        <div class="form-group">
            <label for="edit-imagefile">Image</label>
            <input type="file" id="edit-imagefile" @change="onFileChanged" required/>
        </div>

        <button type="submit" class="btn btn-primary" @click.prevent="updateProduct">Save</button>
        <a class="btn btn-dark"><router-link to="/product-list">Cancel</router-link></a>
    </form>
    </div>
</template>

Vue template

var ProductEdit = Vue.extend({
    template: '#product-edit',
    data: function () {
        return {
            product: findProduct(this.$route.params.product_id),
            selectedImage: null,
        };
    },
    methods: {
        onFileChanged (event) {
            this.selectedImage = event.target.files[0]
            this.product.image_file = this.selectedImage.name
        },
        updateProduct: function () {
            let product = this.product;
            products[findProductKey(product.id)] = {
                id: product.id,
                itemfullhd: product.itemfullhd,
                image_file: product.image_file,
            };
            const data = {
                "Item Name": product.itemfullhd,
                "Item Image File": product.image_file,
            }
            // const formData = new FormData()
            // formData.append('image_file', this.selectedImage, this.selectedImage.name)
            // axios.post('/biggmount_home/Post-Items-Axios', formData, data)

            axios.post('/biggmount_home/Post-Items-Axios', data)
            router.push('/product-list');
        },
    }
});
6
  • So? What's your question? What's the problem you're facing with your current code? an error? unexpected behavior? Commented Oct 2, 2019 at 12:18
  • My problem is how to pass this.selectedImage to django view so that I may use request.FILES in the view OR maybe any other option. Commented Oct 2, 2019 at 12:30
  • request.data['Item Image File'] is empty? I understand what you want, but you should specify your problem. What about your current code is not working? What do see in request.POST? You can't just expect us to read your code and find issues. That's not even how I debug my own code. I use a debugger and my browser debug tools and check what is actually being posted, so I can see where the problem is. Is it the js code? Is the view code? Commented Oct 2, 2019 at 12:33
  • You say "The data is updating properly in my database" so what's the issue? Commented Oct 2, 2019 at 12:36
  • I need to send both the values to the backend [const data] & const formData. Right now I am able to send any one and achieve partial success. const data = { "Item Name": product.itemfullhd, "Item Image File": product.image_file, } // const formData = new FormData() // formData.append('image_file', this.selectedImage, this.selectedImage.name) Commented Oct 2, 2019 at 12:41

1 Answer 1

0

The changes below have given me the result that I was looking to achieve:

Vue template

var ProductEdit = Vue.extend({
    template: '#product-edit',
    data: function () {
        return {
            product: findProduct(this.$route.params.product_id),
            selectedImage: null,
        };
    },
    methods: {
        onFileChanged (event) {
            this.selectedImage = event.target.files[0]
        },
        updateProduct: function () {
            let product = this.product;
            const formData = new FormData()
            formData.append('Item Image', this.selectedImage, this.selectedImage.name)
            formData.append('Item Name', product.itemfullhd)
            axios.post('/biggmount_home/Post-Items-Axios', formData)
            router.push('/product-list');
        },
    }
});

views.py

@api_view(['GET', 'POST', 'PUT', 'DELETE'])
def Post_Items_Axios(request):
    if request.method == 'POST':
        data_itemfullhd = request.data['Item Name']
        td_items, created = Md_Items.objects.get_or_create(
            itemfullhd = request.data['Item Name'],
            )
        td_items.image_file = request.data['Item Image']

        td_items.save()
    return HttpResponse('Success!')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.