2
\$\begingroup\$

I'm starting with new project where there will be used to delete product using AJAX, but sometimes with normal request. What do you think about this view? I'm trying to make it as universal as possible.

@login_required
@require_POST
def delete_products(request):
    products_ids = request.POST.getlist('products_ids')
    deleted = Product.objects.filter(id__in=products_ids,user=request.user).delete()
    if request.is_ajax():
        return JsonResponse({'success':True,
                             'ids':[x.id for x in deleted]})
    return HttpResponseRedirect(request.POST.get('next') or '/')
\$\endgroup\$

0

Your Answer

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

Browse other questions tagged or ask your own question.