jquery upload files and django forms

187
May 05, 2018, at 11:30 PM

Hello everyone i trying add multi upload images to my product form, with jQuery File Upload. At first i need to create product and then add images to this product. If am doing in my views like this:

VIEWS:

def addprod(request):
form = ProductForm
priceforms = ProductPricesFormSet
if request.method == 'POST':
    form = ProductForm(request.POST)
    priceforms = ProductPricesFormSet(request.POST)
    imageform = ProductImageForm(request.POST, request.FILES)
    if form.is_valid() and priceforms.is_valid() and imageform.is_valid():
        # PRODUCT SAVE
        product = form.save(commit=False)
        product.shop_owner = request.user.shop
        product.save()
        # PRICES SAVE
        prices = priceforms.save(commit=False)
        for price in prices:
            price.product = product
            price.save()
        # IMAGE SAVE
        image = imageform.save(commit=False)
        image.product = product
        image.save()
        files = [serialize(image)]
        data = {'files': files}
        response = JSONResponse(data, mimetype=response_mimetype(request))
        response['Content-Disposition'] = 'inline; filename=files.json'
        return response
    else:
        data = json.dumps(imageform.errors)
        return HttpResponse(content=data, status=400, content_type='application/json')
return render(request, 'add_product.html', {'form': form,
                                            'priceforms': priceforms})

i am getting error "The 'file' attribute has no file associated with it."

serializers

def order_name(name):
    name = re.sub(r'^.*/', '', name)
    if len(name) <= 20:
        return name
    return name[:10] + "..." + name[-7:]
def serialize(instance, file_attr='file'):
obj = getattr(instance, file_attr)
return {
    'url': obj.url,
    'name': order_name(obj.name),
    'type': mimetypes.guess_type(obj.path)[0] or 'image/png',
    'thumbnailUrl': obj.url,
    'size': obj.size,
    'deleteUrl': reverse('upload-delete', args=[instance.pk]),
    'deleteType': 'DELETE',
}
READ ALSO
How add hotkeys for video-js in PHP - WordPress Plugin

How add hotkeys for video-js in PHP - WordPress Plugin

this is the plugin php code, here is the hotkey plugin how i can add that? pleas help me! I am very curious to know how?

126
jQuery get ajax response status failing

jQuery get ajax response status failing

I'm experiencing a problem with an ajax call to a different subdomain, the request is sent from wwwmydomain

163
Raty in ruby on rails not showing stars in app

Raty in ruby on rails not showing stars in app

or, jQuery-Ratyjs problem not running in ruby on rails app

99