简体   繁体   English

在Java脚本代码中使用Django STATIC_URL不起作用

[英]Django STATIC_URL use within java script code not working

My problem is that I am using Django STATIC_URL within my java script code. 我的问题是我在Java脚本代码中使用了Django STATIC_URL My code works in my developing environment but when I upload it to pythonanywhere.com it's not working. 我的代码在我的开发环境中有效,但是当我将其上传到pythonanywhere.com时,它不起作用。 I checked my code relative paths it's working but using STATIC_URL it's not working my code looks like this: 我检查了我的代码相对路径是否正常,但是使用STATIC_URL却不正常,我的代码如下所示:

document.write("  <source data-src='{{STATIC_URL}}{{filename}}' type='video/mp4' >");

You should be using something like the following Import staticfiles in your template: 您应该在模板中使用类似于以下“导入静态文件”的内容:

{% load staticfiles %}

Then to access the static dir use 'static': 然后使用“ static”访问静态目录:

<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>

That will point to the static dir you have on the file system and the subdirectory 'my_app'. 这将指向文件系统上的静态目录以及子目录“ my_app”。

So you would have: 因此,您将拥有:

document.write("  <source data-src='{% static "filename" %}' type='video/mp4' >");

Check out https://docs.djangoproject.com/en/1.8/howto/static-files/ . 查看https://docs.djangoproject.com/en/1.8/howto/static-files/

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

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