I am building the project in django with mysql database i am passing the data as context data to template on GET request but when i use
donation.objects.all()
the context data is correctly accessed within the template but when i use
donation.objects.filter()
after passing data as context i am getting empty dict in template
here is my template and js functions
template:
{{data|json_script:"data"}}
js:
function showdonations() {
var donations_data = JSON.parse(document.getElementById('data').textContent);
donations_data = JSON.parse(donations_data);
console.log(donations_data)
}
function getMarkers(Lat, Lng) {
var http = new XMLHttpRequest();
var url = "http://127.0.0.1:8000/foodrequest/donations/" + "?lat=" + encodeURIComponent(Lat) + "&lng=" + encodeURIComponent(Lng);
http.open('GET', url, true);
http.send();
http.onreadystatechange = function () {
if (http.readyState === 4) {
if (http.status === 200) {
alert("OK");
showdonations();
}
else if (http.status === 500) {
alert("Incorrect Data");
}
else {
alert("Error");
}
}
}
}
view:
def donations(request,id=0):
if request.method=='GET':
lat=request.GET.get('lat',40.731)
lng=request.GET.get('lng',-73.997)
minlat=float(lat)-1.0
minlng=float(lng)-1.0
maxlat=float(lat)+1.0
maxlng=float(lng)+1.0
donations_data=donation.objects.filter(Lat__range=[minlat,maxlat],Lng__range=[minlng,maxlng])
# donations_data1=donation.objects.all()
donations_serializer=donationSerializer(donations_data,many=True)
# print(donations_serializer.data)
data={"data":json.dumps(donations_serializer.data)}
return render(request,'donations.html',data)
console:
for objects.all() :
(3) [{…}, {…}, {…}]
0: {Name: 'abcd', UserID: 'abcdabcd', Place_id: 'ChIJexAZtTbB5zsRyA--VJe5UoQ', Lat: '19.0744857000', Lng: '72.997784099999990'}
1: {Name: 'deepak patil', UserID: 'abcdabcd', Place_id: 'ChIJ1RH6XprI5zsR-XiacqeOcpA', Lat: '19.0726295000', Lng: '72.884472100000000'}
2: {Name: 'deepak Patil', UserID: 'adfasdfasd', Place_id: 'adfaldkfjlkasdjfl', Lat: '19.0512620000', Lng: '72.936840000000000'}
length: 3
[[Prototype]]: Array(0)
for objects.filter() :
[]
length: 0
[[Prototype]]: Array(0)
if I print data for object.filter() in the view it is correct but after rendering template it is not showing up.