[Answered ]-Reload template after AJAX POST request in Django

2👍

✅

If you return the output of render_to_response to an ajax call, you are returning the HTML as the “data” element in “success:function(data)” in your javascript call. I don’t think you want to be using AJAX if your goal is to reload the page.

You should have something like:

navigator.geolocation.getCurrentPosition(function(position) {
 var form = $('<form action="/geosearch/" method="POST"></form>');
 var long =$('<input name = "long" type="hidden"></input>');
 var lat =$('<input name = "lat" type="hidden"></input>');
 lat.val(position.coords.latitude);
 long.val(position.coords.latitude);
 form.append(lat, long);
 $('body').append(form);
 form.submit();
}

And your view at /geosearch/ should take the post variables, do whatever you want, and then render_to_response.

Leave a comment