【问题标题】:access file from static folder with template tags inside javascript code使用javascript代码中的模板标签从静态文件夹访问文件
【发布时间】:2020-10-09 06:12:39
【问题描述】:

我有一个从提交按钮创建文件位置的应用程序

json_path = os.path.join('json', request.POST['submit'], '.json' )

这给出了\static\json\ 文件夹下的文件位置。

我正在使用字典将这个发送到模板

render_dict = {
        'json_path':json_path,
    }

在 javascript 中,我有以下内容

<script>

map.addSource('locationData', {
                    type: 'geojson',
                    data: "location of the json file needs to be provided here"
                    });

</script>

谁能建议这是否可以通过模板标记来完成?

【问题讨论】:

    标签: javascript django templates tags


    【解决方案1】:

    我通过以下工作以某种方式解决了这个问题。但如果有更好的建议将不胜感激。

    1. 在 render_dict 我发送一个列表而不是字符串 -

      render_dict = { 'json_path':[json_path], }

    2. 在 javascript 中我使用了以下 -

      map.addSource('locationData', { 类型:'geojson',

      {% autoescape on %} 数据:"{{json_path|safe|escape|cut:"'"|cut:"["|cut:"]"}}" {% endautoescape %}

                  });
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 2013-11-22
      • 2017-12-23
      • 2015-12-23
      • 1970-01-01
      相关资源
      最近更新 更多