[Answer]-Best way to process ajax request in django view

1👍

What about:

    products = [int(p) for p in request.POST['products'] if str(p).isdigit()]

or

    products = [int(p) for p in request.POST['products'] if unicode(p).isnumeric()]

I recommend the first one, because if you use ñ for example unicode will throw an error.

And your code can look like this:

if request.is_ajax():
    products = [int(p) for p in request.POST.get('products',[]) if str(p).isdigit()]
    products_params = request.POST.get('products_params', [])
    if (not (products and products_params)) or (len(products) != len(products_params)):
        return HttpResponseBadRequest()

Leave a comment