V
V
vasy79912018-12-13 14:08:16
Django
vasy7991, 2018-12-13 14:08:16

How to pass data from models to js script?

You need to group the data from the model into a list, for example: points =
and transfer the script to js.
The first value in the list is latitude, the second is longitude. This data is needed to visualize points on the map. If I send the data as I send it below to views.py, then the dots are not displayed. But if I just pass the list that I initialized myself, that is, I entered the data manually, for example: points = , then everything is displayed on the map. I tried to display the list in the view and ended up with .
If I understand correctly, then the cause of the problem may be that instead of a list of the form: points = the list . Question how to make so that the correct list was transferred?
models.py:

from django.db import models
class Station(models.Model):
    idstation = models.IntegerField(db_column='IdStation', primary_key=True)  # Field name made lowercase.
    sitecode = models.CharField(db_column='SiteCode', max_length=4, blank=True, null=True)  # Field name made lowercase.
    namestation = models.CharField(db_column='NameStation', max_length=50, blank=True,
                                   null=True)  # Field name made lowercase.
    latitude = models.DecimalField(db_column='Latitude', max_digits=6, decimal_places=3, blank=True,
                                   null=True)  # Field name made lowercase.
    longitude = models.DecimalField(db_column='Longitude', max_digits=6, decimal_places=3, blank=True,
                                    null=True)  # Field name made lowercase.
    type = models.CharField(db_column='Type', max_length=11, blank=True, null=True)  # Field name made lowercase.
    link = models.CharField(db_column='Link', max_length=150, blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'station'

views.py:
from django.shortcuts import render
from django.views.generic import TemplateView
from .models import Station


def get_point():
    pointlat = Station.objects.values_list("latitude")
    pointlong = Station.objects.values_list("longitude")
    points = []
    i = 0
    while i < len(pointlat):
        points.append([pointlat[i], pointlong[i]])
        i += 1
    return (points)
    # return (point)

class List(TemplateView):
    template_name = 'station_list.html'

    def get(self, request):
        all_stations = Station.objects.values("latitude", "longitude")
        ResponseData = get_point()
        ctx = {
            'all_stations': all_stations, 'ResponseData': ResponseData
        }
        return render(request, self.template_name, ctx)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2018-12-13
@vasy7991

points.append([str(pointlat[i]), str(pointlong[i])])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question