I'm using HTML5 geolocation to get a user's location, then sending the lat/long to my django app to find the three nearest schools. I'm able to post the lat/long, run it through a function to get the closest schools, and print out the dict object in terminal, but the template never reloads with the new context_dict data.
HTML
{% csrf_token %}
<input id = 'button' type="submit" value="Use current location" onclick = 'find_school()' class="btn btn-default">
JS
function find_school(){
function send_off(lat, long){
var locale = [lat, long];
console.log(lat, long);
return locale
}
navigator.geolocation.getCurrentPosition(function(position) {
$.ajax({
type: "POST",
url: "/schools/search/",
data: {
csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
lat_pos: position.coords.latitude,
long_pos: position.coords.longitude
},
success: function(data){
console.log(data);
}
})
});
}
views.py
def find_school(request):
context = RequestContext(request)
search_school_list = search_school_bar()
if request.GET:
address = request.GET['q_word']
close_schools = geolocate(address)
context_dict = {'close_schools': close_schools, 'search_schools':json.dumps(search_school_list)}
elif request.method == 'POST' and request.is_ajax():
position = request.POST
#the geo_search view takes the lat and long
return geo_search(request, position['lat_pos'],position['long_pos'] )
else:
context_dict = {'search_schools':json.dumps(search_school_list)}
return render_to_response('school_data/search.html', context_dict, context)
def geo_search(request, lat, long):
context = RequestContext(request)
search_school_list = search_school_bar()
close_schools = geolocate_gps(lat, long)
context_dict = {'close_schools': close_schools, 'search_schools':json.dumps(search_school_list)}
#This print statement returns in my terminal the results, and they are correct. I just need to reload the template to display the results.
print context_dict
#This isn't re-rendering the page with the correct context_dict. It is doing nothing.
return render_to_response('school_data/search.html', context_dict, context)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.