简体   繁体   English

无法加载串联的静态文件源

[英]Unable to load concatenated static file source

I am new to django and eventually I learnt the use of static files in Django and to my relief I was finally able to load the files while hardcoding the file name in the {%static filename.jpg %}. 我是django的新手,最终我学会了在Django中使用静态文件,而令我欣慰的是,我终于能够在将文件名硬编码在{%static filename.jpg%}中的同时加载文件。 However, when I tried to create a string by replacing the hardcoded filename.jpg with the dynamic file name, I wasn't getting the output. 但是,当我尝试通过将硬编码的filename.jpg替换为动态文件名来创建字符串时,却没有得到输出。

Not working code snippet: 无效的代码段:

 <script> image_name = "1.png" static_start = "{% static '" static_end = "' %}" image_src = static_start.concat(image_name, static_end) window.alert(image_src) var para = document.createElement("img"); {% load static %} para.setAttribute("src", image_src) var element = document.getElementById("div_test"); element.appendChild(para); </script> 

Working Code snippet: 工作代码段:

 <script> var para = document.createElement("img"); {% load static %} para.setAttribute("src", "{%static '1.png'%}") var element = document.getElementById("div_test"); element.appendChild(para); </script> 

What I am trying to do is that, I have a bunch of image files that I am getting from somewhere through an API and I am storing them in the static folder. 我想做的是,我有一堆图像文件是通过API从某处获取的,并将它们存储在静态文件夹中。 After downloading those image, I am trying to load them on my webpage, for that I am using the static file and getting the file name dynamically as I do not know what would the file name of the downloaded file. 下载完这些图像后,我试图将它们加载到我的网页上,因为我正在使用静态文件并动态获取文件名,因为我不知道下载文件的文件名是什么。

The point is that string concatenation isn't working, while directly passing the string is. 关键是字符串串联不起作用,而直接传递字符串却起作用。

Any help would really be appreciated. 任何帮助将不胜感激。

PS: Apparently in the example shared above I am simply using 1.png which would eventually be replaced by the name of the file I wish to display. PS:显然,在上面共享的示例中,我只是使用了1.png,最终将替换为我希望显示的文件名。

Working code using get_static_prefix (the way I actually wanted) 使用get_static_prefix(我实际想要的方式)的工作代码

 <script> image_name = "1.png" var para = document.createElement("img"); {% load static %} para.setAttribute("src", "{% get_static_prefix %}" + image_name) var element = document.getElementById("div_test"); element.appendChild(para); </script> 

I think I understand what you are trying to do here, but I don't think it is possible. 我想我知道您在这里想做什么,但我认为这是不可能的。 Django templates are rendered on the server, and JavaScript is rendered on the client-side. Django模板在服务器上呈现,而JavaScript在客户端上呈现。 You are trying to create the template tag with JavaScript, but the template tags won't be evaluated on the client-side, they will just be strings. 您正在尝试使用JavaScript创建模板标签,但是模板标签不会在客户端进行评估,它们只是字符串。

So after referring to other posts, I realized what I was doing wrong. 因此,在参考其他帖子后,我意识到自己做错了。 I believe there was something that wasn't right about string concatenation and the escape characters and therefore Django has an option for get_static_prefix and that is was I was supposed to use instead of that stupid string concatenation. 我相信对于字符串连接和转义字符来说有些不对劲,因此Django为get_static_prefix提供了一个选项,那就是我应该使用的选项,而不是那种愚蠢的字符串连接。 I have edited my question with the correct working response, exactly the way I wanted it to. 我已经按照我希望的正确方式对问题进行了编辑,并给出了正确的工作答案。

References: Stackoverflow question , Django tutorial 参考: Stackoverflow问题Django教程

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

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