I am not getting correct context data in template

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.

Please don’t post images of code and console output. They’re not readable on many devices, and can’t be highlighted, quoted, or copied to illustrate points.

Please post the code and console text here, with each block of text enclosed between lines of three backtick - ` characters. That means you’ll have a line of ```, then the code (or text), then another line of ```.

Hey ken I have edited the topic with code snippets

What are the lat/lng that you are supplying in your test?
(What is the output if you print lat and lng in your view after the gets?)

lat =19.0512616
lng=72.9368399
and I am getting the data in view after objects.filter() but in template I am getting empty set

The output that you’ve posted shows no elements when you use the filter. I don’t understand how that fits with what you’re saying here.

What does your donations model look like?

the output I have posted is console output of javascript i.e when I access the data from template in js and do console.log() it is printing like []

class donation(models.Model):
    Name = models.CharField(max_length=100)
    UserID = models.CharField(max_length=50)
    Place_id = models.CharField(max_length=200)
    Lat=models.DecimalField(max_digits=12,decimal_places=10,null=True)
    Lng=models.DecimalField(max_digits=18,decimal_places=15,null=True)

this is the donation model

So what is the output from the print statement in the views in both cases? (The print statement currently commented out.)

If you’re saying that it’s not rendering correctly, then nothing in the JavaScript is relevant here. If the rendered html isn’t right, then the JavaScript won’t be right either. Did you visually examine the html element being rendered?

it is not rendering in html also

this is the print statement output in view for
print(lat,lng)
and
print(donations_serializer.data)

System check identified no issues (0 silenced).
February 18, 2022 - 11:57:02
Django version 4.0, using settings 'HungerFree.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
40.731 -73.997
[]
[18/Feb/2022 11:57:08] "GET /foodrequest/donations/ HTTP/1.1" 200 4099
[18/Feb/2022 11:57:09] "GET /static/css/donationpage.css HTTP/1.1" 200 115
[18/Feb/2022 11:57:09] "GET /static/donations.js HTTP/1.1" 200 2825
Not Found: /favicon.ico
[18/Feb/2022 11:57:09] "GET /favicon.ico HTTP/1.1" 404 2649
19.0512616 72.9368399
[OrderedDict([('Name', 'abcd'), ('UserID', 'abcdabcd'), ('Place_id', 'ChIJexAZtTbB5zsRyA--VJe5UoQ'), ('Lat', '19.0744857000'), ('Lng', '72.997784099999990')]), OrderedDict([('Name', 'deepak patil'), ('UserID', 'abcdabcd'), ('Place_id', 'ChIJ1RH6XprI5zsR-XiacqeOcpA'), ('Lat', '19.0726295000'), ('Lng', '72.884472100000000')]), OrderedDict([('Name', 'deepak Patil'), ('UserID', 'adfasdfasd'), ('Place_id', 'adfaldkfjlkasdjfl'), ('Lat', '19.0512620000'), ('Lng', '72.936840000000000')])]
[18/Feb/2022 11:57:11] "GET /foodrequest/donations/?lat=19.0512616&lng=72.9368399 HTTP/1.1" 200 4813

Notice the minus sign in the lng. The values you’re showing in the data are positive. The filters are not going to return results. Also note those lat/lng don’t match what you’re passing.

the negative sign longitude is first GET request in which i haven’t passed lat and lng so it is using default values.

check at the end where I have passed lat and lng which is 19.0512616 & 72.9368399 for which I am getting the data

Ok, I’ve got a hunch here. This output that you’ve shown, that’s with the filter? If so, please show the same output with the .all().

this is for .all()

System check identified no issues (0 silenced).
February 18, 2022 - 12:13:30
Django version 4.0, using settings 'HungerFree.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
40.731 -73.997
[OrderedDict([('Name', 'abcd'), ('UserID', 'abcdabcd'), ('Place_id', 'ChIJexAZtTbB5zsRyA--VJe5UoQ'), ('Lat', '19.0744857000'), ('Lng', '72.997784099999990')]), OrderedDict([('Name', 'deepak patil'), ('UserID', 'abcdabcd'), ('Place_id', 'ChIJ1RH6XprI5zsR-XiacqeOcpA'), ('Lat', '19.0726295000'), ('Lng', '72.884472100000000')]), OrderedDict([('Name', 'deepak Patil'), ('UserID', 'adfasdfasd'), ('Place_id', 'adfaldkfjlkasdjfl'), ('Lat', '19.0512620000'), ('Lng', '72.936840000000000')])]
[18/Feb/2022 12:13:47] "GET /foodrequest/donations/ HTTP/1.1" 200 4813
19.0512616 72.9368399
[OrderedDict([('Name', 'abcd'), ('UserID', 'abcdabcd'), ('Place_id', 'ChIJexAZtTbB5zsRyA--VJe5UoQ'), ('Lat', '19.0744857000'), ('Lng', '72.997784099999990')]), OrderedDict([('Name', 'deepak patil'), ('UserID', 'abcdabcd'), ('Place_id', 'ChIJ1RH6XprI5zsR-XiacqeOcpA'), ('Lat', '19.0726295000'), ('Lng', '72.884472100000000')]), OrderedDict([('Name', 'deepak Patil'), ('UserID', 'adfasdfasd'), ('Place_id', 'adfaldkfjlkasdjfl'), ('Lat', '19.0512620000'), ('Lng', '72.936840000000000')])]
[18/Feb/2022 12:13:55] "GET /foodrequest/donations/?lat=19.0512616&lng=72.9368399 HTTP/1.1" 200 4813

and for .all() I am getting the data in html as well as js

What happens if you replace that with:
data={"data":donations_serializer.data}

for .all() only difference is it is giving ordereddict as output in template
and for .filter() its same []

This isn’t making sense. I think I’m going to need to try and recreate this behavior.

Okay ,thanks for the help
Just tell me is there any other way to pass the data along with template

Honestly, if this is an AJAX call, I wouldn’t be rendering it. I’d be returning the data directly as a JsonResponse and not as an HttpResponse.