简体   繁体   English

将数据从 Django 视图/模板传递到静态 javascript

[英]Passing data from Django views/template into the static javascript

This question has been asked in multiple ways , however the solutions such as defining variables before the script tag haven't worked.这个问题已经以多种方式被问到,但是诸如在脚本标签之前定义变量之类的解决方案没有奏效。

template.html模板.html

<script>
    var locations = {{ some_variable }}
</script>
<script type="text/javascript" src="{% static 'base/scripts/maps.js' %}"></script>

I believe this is because maps.js is actually a function.我相信这是因为maps.js实际上是一个函数。 The start of it is as follows:它的开头是这样的:

maps.js地图.js

(function($){
    "use strict";

    function mainMap() {
      var ib = new InfoBox();

       // Infobox Output
       function locationData(locationURL,locationImg,locationTitle, locationAddress, locationRating, locationRatingCounter) {
           return('SOME_HTML_WITH_VARIABLES')
      }

       // Locations
       var locations = [
         [ locationData('listings-single-page.html','images/listing-item-01.jpg',"Tom's Restaurant",'964 School Street, New York', '3.5', '12'), 40.94401669296697, -74.16938781738281, 1, '<i class="im im-icon-Chef-Hat"></i>'],

       ];

What I want to do is to pass location data from my model eg我想要做的是从我的模型中传递位置数据,例如

  {% for model in query_list %}
[ locationData('link_to','img_url','{{ model.name }}','{{ model.address }}', '5', '10'), {{ model.latitude }}, {{ model.longitude }}, {{ forloop.counter }}, '{{ model.icon }}'],
  {% endfor %}

Possible solution:可能的解决方案:

Post the whole maps.js file in the template.html, inside the script tag.将整个maps.js文件发布到 template.html 中的脚本标签内。 But this file has 10000+ lines.但是这个文件有 10000 多行。

This is not an exact solution, but it is a trick to handle the situation.这不是一个精确的解决方案,但它是处理这种情况的一个技巧。

views.py视图.py

def index(request):
    monthList = ['jan', 'feb', 'mar]
    activities = [1,2,3]
    model = {"name" : "Fine Guy" ]

    context = {
      'monthList' : monthList,
      'activities' : activities,
      'model' : model,
    }

    return render(request, 'index.html', context)     

index.html索引.html

<!-- passing django values to external js -->
<!-- hided -->
<input type="text" id="monthList" style="display: none" value="{{ monthList|safe }}">
<input type="text" id="activities" style="display: none" value="{{ activities|safe }}">

<!-- for your question -->
<input type="text" id="modelname" style="display: none" value="{{ model.name|safe }}">

Here, I just pass the list of values monthList and activities from the django context to the rendered HTML file, and I hide that using (style="display:none")在这里,我只是将值列表monthListactivities从 django 上下文传递到呈现的 HTML 文件,并使用(style="display:none")隐藏它

|safe is for accurate value from the Python format to the Javacript format |safe是为了从 Python 格式到 Javacript 格式的准确值

Now, you can get the values in static/external.js using the tag Id from index.html现在,您可以使用index.html中的标签 Id获取static/external.js中的值

external.js外部.js

var monthList = document.getElementById("monthList").value;
var activities = document.getElementById("activities").value;

// for your question
var modelname = document.getElementById("modelname").value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM