107👍
✅
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
6👍
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_POST
from django.http import JsonResponse
@require_POST
def yourUrlName(request):
array = request.POST.getlist('pk[]')
return JsonResponse({
'status':'ok',
'array': array,
})
- [Django]-How to return HTTP 400 response in Django?
- [Django]-Data Mining in a Django/Postgres application
- [Django]-Django: How to check if the user left all fields blank (or to initial values)?
- [Django]-Django/DRF – 405 Method not allowed on DELETE operation
- [Django]-Add rich text format functionality to django TextField
- [Django]-How to run Django's test database only in memory?
1👍
With newer versions of django I was finding that even @YellowCap suggestion of request.POST.getlist('data[]')
wasn’t working for me.
However dict(request.POST)["data"]
does work. Context here:
https://code.djangoproject.com/ticket/1130
- [Django]-Django Rest Framework writable nested serializers
- [Django]-Django – after login, redirect user to his custom page –> mysite.com/username
- [Django]-Where is a good place to work on accounts/profile in Django with the Django registration app?
Source:stackexchange.com