How can i pass data from template in Django to Javascript in this specific case

2024/11/16 13:56:51

I am using Google map api and i am trying to pass the data (longitude and latitude) to the template then use the data in the javascript to show a specific location.

location.html

{% for venue in property_listing %}{{ venue.address }}</br><div id="long">{{ venue.longitude }}</br><div id="lat">{{ venue.latitude }}</br>
{% endfor %}

javascript of the same page

<script>var latitude = REPLACE HERE;var longitude = REPLACE HERE;// Initialize and add the mapfunction initMap() {// The location of Uluruvar uluru = {lat: latitude, lng: longitude};// The map, centered at Uluruvar map = new google.maps.Map(document.getElementById('map'), {zoom: 15, center: uluru});// The marker, positioned at Uluruvar marker = new google.maps.Marker({position: uluru, map: map});}</script>

I am tried to replace the value literally but it wont work. What is the best way to solve this?

Answer

First of all you are assigning your data into a div. Which doesn't have a proper value attribute. Here is a work around by using getAttribute() method.

Assign an attribute named 'value' and it's corresponding data:

location.html

{% for venue in property_listing %}{{ venue.address }}</br><div id="long" value="{{ venue.longitude }}">{{ venue.longitude }}</br><div id="lat" value="{{ venue.latitude }}">{{ venue.latitude }}</br>
{% endfor %}

In your javascript function, access the data by getting the attribute of your div ids named value:

<script>var latitude = document.getElementById('lat').getAttribute("value");var longitude = document.getElementById('long').getAttribute("value");// Initialize and add the mapfunction initMap() {// The location of Uluruvar uluru = {lat: parseFloat(latitude), lng: parseFloat(longitude)};// The map, centered at Uluruvar map = new google.maps.Map(document.getElementById('map'), {zoom: 15, center: uluru});// The marker, positioned at Uluruvar marker = new google.maps.Marker({position: uluru, map: map});}
</script>
https://en.xdnf.cn/q/119187.html

Related Q&A

Python Highscores w/ text file

i am currently working on a text based game, and have run into a problem trying to make my last function, highscore. My problem is this, i would like to make the function save my top five scores and sa…

Do any one know about this Error in python? how can I resolve this?

I am plotting the data with MapBoxGl Python Library on maps, here is my code which is taking the latitude, longitude and points from the Pandas DataFrame and trying to make the geojson, here is the cod…

Pandas: Count Higher Ranks For Current Experiment Participants In Later Experiments (Part 1)

Learning Experiments In a series of learning experiments, I would like to count the number of participants in each experiment that improved their performance in subsequent experiments (Rank 1 is highes…

Pass variables into and out of exec [closed for not being clear. Modified and reuploaded] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

List Object Not Callable, Syntax Error for Text-Based RPG

Im having issues (still) with generating a random object, in this case a random herb found by foraging. Heres the code for the function:def collectPlants(self):if self.state == normal:print"%s spe…

Why does my Tkinter window background not change?

Ive a small program with a feature to change the background color of a different window than the frame I use to ask for the background color. (I hope you understand this.) The program is written in Pyt…

Combining a nested list without affecting the key and value direction in python

I have a program that stores data in a list. The current and desired output is in the format:# Current Input [{Devices: [laptops, tablets],ParentCategory: [computers, computers]},{Devices: [touch], Par…

Splitting very large csv files into smaller files

Is Dask proper to read large csv files in parallel and split them into multiple smaller files?

How to make this Python script to run subfolders too?

Which part of the codes do I need to change in order to include subfolders? File handle.py import glob import os import sys from typing import Listdef get_filenames(filepath: str, pattern: str) -> …

Why doesnt this recursive GCD function work? [duplicate]

This question already has answers here:Why does my recursive function return None?(4 answers)What is the purpose of the return statement? How is it different from printing?(15 answers)Closed 1 year …