Created on Dec. 13, 2012, 4:44 p.m. by Hevok & updated on Dec. 13, 2012, 4:47 p.m. by Hevok
If a site needs to be very dynamic, i.e. it needs to change its content without changing the url or refreshing the page, JavaScript can be employed.
¶
¶
Basically onBlur or onChange or some event function call a python code to take some arguments and inserts that data in the database.
¶
¶
To accomplish this some id need to be added to a template field and then jQuery selector used to get the value from it on blur or input.
¶
¶
To make the Ajax call use jQuery's .post() [http://api.jquery.com/jQuery.post/]:
¶
¶
.. sourcecode:: js
¶
¶
$('#some_id').omn("blur", function(){
¶
// Make ajax call here
¶
$.post('a-url/', function(data) {
¶
$('result'.html(data); // Do what needs to be done with the returned data.
¶
});
¶
});
¶
¶
Create an url and a view to capture that post:
¶
¶
.. sourcecode:: python
¶
¶
@csrf_exempt
¶
def view(request:
¶
if request.is_ajax() and request.method == 'POST':
¶
captured_post_parameter = request.POST.get('field_name')
¶
# Do logic here
¶
# Return either HttpResponse(response) or use simplejson to return json response
¶
¶
¶
¶
¶
Comment on This Data Unit