1

I am stuck using <script> tags to render a googlemaps api window in my django webapp. In my views I passed my data as such:

def map_view(request):
    context = {
        'title': 'My Map',
        'machines': Machine.objects.all(),
    }

    return render(request, 'gui/map_view.html', context)

I simply want to leverage some parameters contained in the Machine objects (strings) to automate generation of Markers on the map, but can't figure out how to use machines in the javascript code. Tried both {{ }} and {% %}

4
  • machines is a collection of Machine objects, you can not "automagically" turn it in something JavaScript understands. Commented May 11, 2020 at 20:03
  • Can you show exactly how you want to transform these Machine objects in something a Google Maps API will find useful? Commented May 11, 2020 at 20:04
  • Just want to use some parameters from the machine object, strings and ints to use in the InfoWindow Commented May 11, 2020 at 20:07
  • Also I got longitude and latitude as Machine's parameters, would like to use those to auto generate more Marker objects in JS Commented May 11, 2020 at 20:09

4 Answers 4

0

If I understand you correctly, then you are trying to get the location data stored in every machine into some sort of JavaScript collection.

The way you are doing it now probably requires a template somewhat like this

<script>
  var machines = [ // define list with machine positions
  {% for machine in machines %} // use django templates to interact with the passed context
    machine.position, // I don't know what your Machine class looks like, but you get the idea
  {% endfor %}
  ];
</script>

Read about the template language here

I suspect this is not the best way to achieve this. Maybe try generating the list as a string in python and passing that in the context instead.

Sign up to request clarification or add additional context in comments.

3 Comments

This is what I would want to be able to do, but it doesn't work on my side. Looking for a workaround
"Doesn't work" is a little vague. Are you getting any errors? Is it generating the list with the wrong syntax? Is the generated list empty?
The code doesn't compile, but even it it did, the variable machines inside the for loop does not exists.
0

I am still learning, so I hope this help you, I had a django project in which I had a webpage that displayed a list of movies, this was the views.py code:

def movies_list(request):
    movies = Movie.objects.all().order_by('pk')
    return render(request, 'movies/movies_list.html', {'movies':movies, 'title':'Movie List'})  

The HTML part is like this:

{% extends 'base_layout.html' %}
{% block content %}
    <div class="container">
        <div class="row">
            {% for movie in movies %}
            <a href="{% url 'movie_detail' pk=movie.pk %}" >
                <div class="col cell"><img src="{{ movie.movie_poster.url }}"></div>
            </a>
            {% endfor %}

        </div>

    </div>
{% endblock %}  

So I passed two parameters to the html 1 the page title, and the second the movies object, and I can use it's fields.

2 Comments

Yes, this isn't a problem for me. My issue is that to create the GoogleMap feature in my webapp, I need to write Javascript code. And would need to use my {{ data }} in JS, but I cannot directly
0

A handy trick I've used over the years... in your view:

from json import dumps

def map_view(request):
    context = {
        'title': 'My Map',
        'machines': dumps(Machine.objects.all()),
    }

    return render(request, 'gui/map_view.html', context)

Then in your template, in JavaScript:

<script>
var machines = {{ machines }}

console.log(machines)
</script>

Good luck!

1 Comment

Nope, please avoid this! It is vulnerable to Cross-site scripting (XSS) attacks.
0

I know its kinda late but for future reference, for those who don't want solely rely on AI. I've solve mine by doing this on the script tag:

const contextName = `{{ contextName }}`;
console.log(contextName);

I hope this help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.