OnChange

Created on Dec. 13, 2012, 4:44 p.m. by Hevok & updated by Hevok on May 2, 2013, 5:25 p.m.

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/]:

$('#some_id').on("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:

@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
onchange.jpg

Tags: js, coding, rest
Categories: Tutorial, reST
Parent: Web Framework

Update entry (Admin) | See changes

Comment on This Data Unit