How to get an array in Django posted via Ajax
You're looking for the QueryDict
's getlist
request.GET.getlist('data')request.GET.getlist('data[]')request.GET.getlist('etc')
https://docs.djangoproject.com/en/2.0/ref/request-response/#django.http.QueryDict.getlist
Quite old question but let me show you full working code for this. (Good for newbie :)
In your template
data = { 'pk' : [1,3,5,10]}$.post("{% url 'yourUrlName' %}", data, function(response){ if (response.status == 'ok') { // It's all good console.log(response) } else { // Do something with errors } })
urls.py
urlpatterns = [ url(r'^yourUrlName/', views.yourUrlName, name='yourUrlName'), #Ajax]
views.py
from django.views.decorators.http import require_POSTfrom django.http import JsonResponse@require_POSTdef yourUrlName(request): array = request.POST.getlist('pk[]') return JsonResponse({ 'status':'ok', 'array': array, })