简体   繁体   English

找不到django文件错误:当静态目录中的javascript文件试图访问静态目录中的另一个文件时?

[英]django file not found error: when javascript file inside static directory trying to access another file in the static directory?

I am doing one django project. 我正在做一个django项目。 And I am able to access the "custom.js" file from static folder path.This is a line inside my "index.html" file. 而且我可以从静态文件夹路径访问“ custom.js”文件。这是我的“ index.html”文件中的一行。

<script type="text/javascript" src="{% static 'crypto_app/js/custom.js' %}" ></script>

But inside "custom.js" file it is using this : 但是在“ custom.js”文件中,它使用的是:

$(window).ready(function() {
'use strict';
$.vegas('slideshow', {
  backgrounds:[
    { src: 'images/bg-slider/bg-1.jpg', fade:1000 },
    { src:'images/bg-slider/bg-2.jpg', fade:1000 },
    { src:'images/bg-slider/bg-3.jpg', fade:1000 }
})();

And hence, due to wrong file address, I am not able to access the images. 因此,由于文件地址错误,我无法访问图像。 And it is showing file not found. 并且它显示找不到文件。

Is there some django way to declare path of images as variable and accessing it from the "custom.js" file? 是否有某种django方式将图像的路径声明为变量并从“ custom.js”文件访问它? Given below is my directory structure : 下面是我的目录结构:

|   admin.py
|   apps.py
|   models.py
|   tests.py 
|   tree.txt
|   urls.py
|   views.py
|   
+---migrations
|   |   __init__.py|           
+---static
|   \---crypto_app      
|       +---css
|       |           
|       +---fonts      
|       +---images  
|       |   \---bg-slider
|       |           bg-1.jpg
|       |           bg-2.jpg
|       |           bg-3.jpg
|       |           
|       \---js
|               custom.js               
+---templates
|   \---crypto_app
|           index.html

First to answer your question, no there is no Django specific thing that makes you get paths for these images OOTB in your JavaScript file. 首先回答您的问题,没有Django特定的东西使您在JavaScript文件中获得这些图像OOTB的路径。 Django would force you to access via HTML (as shown in example #2). Django会强迫您通过HTML进行访问(如示例2所示)。

There's most likely some Django app that you can install. 您最有可能安装一些Django应用。

Now on to solving your conundrum. 现在解决您的难题。 Since you're using the {% static %} template tag to load your JavaScript you would need to do something similar. 由于您使用的是{% static %}模板标签来加载JavaScript,因此您需要执行类似的操作。

Of course there's a few ways to make this happen: 当然,有几种方法可以实现这一目标:

1) Hardcode the URL, {% static %} basically concats your STATIC_URL together with the path you've supplied. 1)对URL进行硬编码, {% static %}基本上将您的STATIC_URL以及您提供的路径STATIC_URL在一起。 So take the same URL and append it to the image urls. 因此,请使用相同的URL并将其附加到图像URL。 Assuming STATIC_URL is set to /static/ your new hardcoded urls would be something along the lines of /static/images/bg-slider/bg-1.jpg and so on. 假设STATIC_URL设置为/static/新的硬编码url /static/images/bg-slider/bg-1.jpg等。

2) Create an options object and tack it onto window . 2)创建一个选项对象并将其添加到window In your index.html : 在您的index.html

<script>
    window.options = {
        staticUrl: {{ STATIC_URL }},
    };
</script>

Then use this in your JS code 然后在您的JS代码中使用它

backgrounds:[
    { src: `${window.options.staticUrl}images/bg-slider/bg-1.jpg`, fade:1000 },
}

Naturally if you have just the one place you do this, hardcoding would be the easiest option for you at this point in time. 自然,如果只有一个地方可以做到这一点,那么此时硬编码将是最简单的选择。 :) :)

Happy hacking! 骇客入侵!

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

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