2👍
✅
You’d need to use simplejson.loads
, for example if you’d pass the anSelected array as arr
you’d use something like this
from django.utils import simplejson
array = simplejson.loads(request.POST['arr'])
try:
ModelName.objects.filter(pk__in=array).delete()
except:
return HttpResponse(simplejson.dumps({'ok': False}))
return HttpResponse(simplejson.dumps({'ok': True}))
and in your javascript this something along these lines:
$.post(
'/delete/',
{arr: anSelected},
function(data){
if(data.ok){
//Everything went smoothly
}else{
//Something failed, you can send extra info from django like the name of the exception thrown if you'd want and display it
}
}
);
6👍
You could try sometings like this:
$(function(){
$.post("{% url delete %}", {"ids[]": anSelected}, function(res){
if(res.ok){
// remove rows from your table, maybe :)
// oTable.fnDeleteRow(anSelected);
}else{
alert(res.errors);
}
});
})
On the server:
@ajax_request
def test(request):
ids = request.POST.getlist("ids[]")
try:
Items.objects.filter(id__in=ids).delete()
except:
return {"ok": False, "errors": "your error"}
return {"ok": True}
the @ajax_request decorators is from https://bitbucket.org/offline/django-annoying/wiki/Home and let you return json response.
- [Django]-Changing django-storages backend from from s3 to cloudfiles and dealing with old files
- [Django]-Django(Python) AttributeError: 'NoneType' object has no attribute 'split'
- [Django]-Error running manage.py syncdb for first time on Heroku: "You need to specify NAME in your Django settings file"
- [Django]-Using Django and Bootstrap Studio together
- [Django]-Front-end prototype/skeleton in Django
Source:stackexchange.com