Ad
Why Template Recognises Object As Unexpected Identifier?
I am trying to populate multiple markers on google maps, using django. From Django views.py I am passing listing name and coordinates to the template, I receive coordinates and can use it as variable in JavaScript. But for some reason, listing name gets error saying "Uncaught SyntaxError: Unexpected number". I can't figure it out.
tried to send it as a string, but still got the same.
Template code:
var single_listing_coordinates = [];
// Function retrieves listing names and coordinates from View Class and stores them inside the single_listing_coordinates list,
// inserts list into hotels array and clears the single_listing_coordinates array, so it is empty for next hotel to be stored.
{% for coordinate in coordinates %}
var a = {{coordinate.listing_lat}};
single_listing_coordinates.push(a);
var b = {{coordinate.listing_lng}};
single_listing_coordinates.push(b);
var c = {{ coordinate.listing_name }};
single_listing_coordinates.push(c);
//Lists of coordinates gets pushed to listings array.
listings.push(single_listing_coordinates);
single_listing_coordinates = [];
{% endfor %}
views.py code :
class ShowMapView(TemplateView):
template_name = 'listing_data/show_maps.html'
def test_func(self, user):
return (not user.is_anonymous) and (
user.is_superuser or is_user_host(user) or is_user_cohost(user) or is_user_housekeeper(user))
def get_context_data(self, **kwargs):
listings_id_parameter = self.request.GET['listings_id']
listing_ids_list = listings_id_parameter.split(",")
context = {}
listing_ids_list = [int(x) for x in listing_ids_list]
all_locations = ABListingData.objects.filter(listing__pk__in=listing_ids_list)
coordinates_list = []
for location in all_locations:
coordinates_dict = {
"listing_name": location.listing.name,
"listing_lat": location.lat,
"listing_lng": location.lng
}
coordinates_list.append(coordinates_dict)
context['coordinates'] = coordinates_list
return context
that's the error I get:
var a = 51.526989;
single_listing_coordinates.push(a);
var b = -0.078021;
single_listing_coordinates.push(b);
var c = Flat 8; Uncaught SyntaxError: Unexpected number
single_listing_coordinates.push(c);
Ad
Answer
Because that's not valid Javascript. As with Python, you need to wrap strings with quotes.
var c = "{{ coordinate.listing_name }}";
Note, this would all probably be easier if you sent JSON from the view and parsed it in the JS:
context['coordinates'] = json.dumps(coordinates_list)
...
var listings = JSON.parse("{{ coordinates|safe }}");
then you don't need to loop in JS at all.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad