I transfer the data set from the server:

from django.shortcuts import render .... context.update({ 'request': request, 'model_class' : model_class, 'id': model_class.id, 'coords_x': coords_x, # например, 40.982 (float) 'coords_y': coords_y, # например, 50.982 (float) 'icon': model_icon, # например, "http://image.flaticon.com/icons/svg/126/126482.svg" (unicode) }) return render(request, 'app/mypage.html', context) 

In app/mypage.html data comes in a strange way. In float numbers, a dot changes to a comma, which is why the script does not perceive it either as a string or as a number, and gives an error not only when trying to process data, but also when converting to float or string . This problem can be circumvented by passing in the context not coords_x , but str(coords_x) , then the point will be preserved and it will be possible to convert this to a number on the client. But it looks weird. And I can not solve the problem with model_icon : the browser does not accept the url string after the colon. Uncaught SyntaxError: Unexpected token : Even if the colon is removed from the transmitted string, the same error occurs. What you need to read to figure out why this is happening?

A non-working piece from app/mypage.html :

  <div class='item_map'> {% leaflet_map "map" callback='map_init_point' %} </div> <script type="text/javascript"> 'use strict'; function map_init_point (map, options){ var coords = []; coords.push(parseFloat({{ coords_x }})); coords.push(parseFloat({{ coords_y }})); if ({{ icon|safe }}){ // вот эта строка никак не хочет работать // в консоли браузера она выглядит как // if (image.flaticon.com/icons/svg/46/46045.svg){ , // а переменная должна быть в кавычках var Point_Icon = L.icon({ iconUrl: 'http://' + {{ icon|safe }}, iconSize: [20, 20], iconAnchor: [10, 10], }); L.marker(coords, {icon: Point_Icon}).addTo(map); } else { L.marker(coords).addTo(map); } map.setView({{ center }}, {{ zoom }}-3); setTimeout(function(){ window.print(); }, 2000); }; 

    2 answers 2

    You can read about automatic shielding

    And the fact that the dot is replaced with a comma, it is possible to increase the readability of the jung doing something, but operations, however, should be available as with a normal number.

    By default in Django, each template escapes all variables. In particular, such replacements are performed:

     < заменяется на &lt; > заменяется на &gt; ' (одинарная кавычка) заменяется на &#39; " (двойная кавычка) заменяется на &quot; & заменяется на &amp; 

    To disable auto-shielding for individual variables, use the safe filter:

     This will be escaped: {{ data }} This will not be escaped: {{ data|safe }} 
    • Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of checks - Urmuz Tagizade
    • Now I will correct, the author simply asked what to read. - Mr Fix
    • could write in the comments. reputation allows - Urmuz Tagizade
    • @Mrfix | safe does not work. There are no escaped characters in the icon. if ({{ icon|safe }} != ''){ produces a line in the browser: if (http://image.flaticon.com/icons/svg/126/126482.svg != ''){ and Uncaught SyntaxError: Unexpected token : error Uncaught SyntaxError: Unexpected token : - Mae
    • Write without if, ok. - Mr Fix

    Django gave the contents of the line. There are no quotes in it, so the variable is passed to the script without quotes. There are several solutions here.

    1) pass in context variable wrapped in quotes.

    'icon': '"'+model_icon+'"',

    2) to wrap the resulting variable in quotes already in the template. if ("{{ icon|escapejs }}"){ In this context, simply the 'icon': model_icon,