I'm trying to implement a quite easy ajax call but since its my first one, something goes wrong and I can't find why. After a users choice from a dropdown list I want another field of the form to be completed after the corresponding query. Here is my code: jquery:
<script type="text/javascript">
$(document).ready(function() {
$('#id_veh_id1').bind('click', function () {
var val1 =$("#id_veh_id1").val();
$.get(""+val1+"/", function(data) {
result=data.veh_length;
document.getElementById('id_vlength').value=result;
});
});
});
</script>
with url: url(r'^(?P<user_id>\d+)/main_Webrequests/(?P<veh_id>\d+)/$', 'auth.views.test', name='test')
and view:
def test(request, veh_id, user_id):
message = {"veh_length": ""}
if request.is_ajax():
vehicle1 = Vehicles.objects.get(id = veh_id)
veh_length = vehicle1.vlength
message['veh_length'] = vehicle1.vlength
else:
message = "yoohoo"
json = simplejson.dumps(message)
return HttpResponse(json, mimetype='application/json')
When I try it with veh_id
instead of veh_length
everything seems to work fine but maybe I didn't understand sth well with the whole thing.