Reputation: 7917
I am sending a variable to jquery side from my view. And what i want is taking and using that value in my js file. i tried $.post or response.xx but didn't work. here is my related view part:
try:
post = Post(title=title, body=body, isdraft=isdraft, owner=owner)
post.save()
except:
post = None
json_dump = simplejson.dumps(post.id)
return HttpResponse(json_dump, mimetype='application/json')
i can see post.id
in firebug's response part. but how can i use that value in my js file.what i am trying to do is like:
var auto_save_id = $('#auto_save_id').val();
if (VALUE_FROM_JSON)){
auto_save_id = VALUE_FROM_JSON;
}
thank you
edit : here is my all related view part:
if 'auto_save_id' in request.POST:
if request.POST['auto_save_id'] == "0":
title = request.POST['title']
body = request.POST['body']
get_isdraft = request.POST['isdraft']
if get_isdraft == "True":
isdraft = True
else:
isdraft = False
owner = request.user
if request.is_ajax():
try:
post = Post(title=title, body=body, isdraft=isdraft, owner=owner)
post.save()
except:
post = None
json_dump = simplejson.dumps(post.id)
return HttpResponse(json_dump, mimetype='application/json')
else:
post_id = request.POST['auto_save_id']
title = request.POST['title']
body = request.POST['body']
get_isdraft = request.POST['isdraft']
if get_isdraft == "True":
isdraft = True
else:
isdraft = False
owner = request.user
post = Post.objects.get(id=eval(post_id))
if request.is_ajax():
post.title = title
post.body = body
post.isdraft = isdraft
post.owner = owner
post.save()
json_dump = simplejson.dumps(post.id)
return HttpResponse(json_dump, mimetype='application/json')
i have a hidden input which value is "0". on first load of page that value is "0" 10 secs later auto_save function runs and send data to view. in view , i check if the auto_save_id = 0: if it is , i create new post and send that post's id to template side. after that on every 10 secs ajax must UPDATE that post. to prevent create new post on each 10 secs, i need saved post's id.
i hope this is clear.
Upvotes: 0
Views: 759
Reputation: 8285
First, I think you want to send a dictionary to simplejson.dumps()
like:
json_dump = simplejson.dumps({'pid': post.id})
Then, in your javascript function, you would get the id with your argument.pid like:
function myCallback(data){
alert(data.pid);
}
Upvotes: 1