简体   繁体   中英

Using google map api(javascript) with django queryset, I can't access queries well of the queryset

I can't access queries of queryset. Only, I can have the first query of queryset, if I do like this.

views.py

def restaurant(request, pk):
    restaurants = Restaurant.objects.all()
    ctx = {
        'restaurants’: restaurants
    }
    return render(request, 'base_app/restaurant.html', ctx)

base.html

    var markers =[];

    // new map
    var map = new
    google.maps.Map(document.getElementById('map'), options);
    {% for restaurant in restaurants %}
        var marker = new google.maps.Marker({
            position: {{ restaurant.locate }},
            map: map,
        });

        var restaurant_introduction =
            '<div class="restaurant_set">\n' +
            ' <div class="restaurant_image"></div>\n' +
            ' <div class="restaurant_content">\n' +
            '   <div class="restaurant_name">{{ restaurant }}</div>\n' +
            ' </div>\n' +
            ' <a href="{% url "base_app:restaurant" pk=restaurant.pk %}"><div class="comments">more</div></a>\n' +
            '</div>';

        markers.push(marker);
    {% endfor %}
}

Do I have to use Json?? OR what should I do to use other queries(I mean other restaurants, not only first one).

Within your view, you're only passing 'comments' as context, but you're calling 'restaurants' in your html. If you want to call 'restaurants' in your html, you have to pass it in as context within your view first.

You need to assign some value to restaurants (likely a queryset), and then define your context like this

ctx = { 'comments': comments, 'restaurants': restaurants, }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM