简体   繁体   English

Django到JavaScript的路径无效

[英]Django pathing to javascript doesn't work

I've been trying all sorts of things and can't figure this out! 我一直在尝试各种事情,无法解决!

For some reason on the Django development server the paths to the JavaScript just don't work. 由于某些原因,在Django开发服务器上,JavaScript的路径不起作用。

Directory structure is 目录结构是

             site
               |
 appName    static      templates
    |          |            |
 views.py  javascript    appName
               |            |
            script.js     index.html

In index.html I have 在index.html中,我有

<script type="text/javascript" src=../../static/javascript/script.js></script>

And it doesn't work! 而且它不起作用!

If I copy and paste the script.js directly into index.html all of the functionality works, just the pathing is messed up. 如果我将script.js直接复制并粘贴到index.html中,那么所有功能都可以正常工作,只是路径混乱了。

Django does not serve static assets by default. Django默认不提供静态资产。 It is possible to make it do it, in the development environment only - see the documentation . 仅在开发环境中可以做到这一点-请参阅文档

How about: 怎么样:

src="/static/javascript/..."

Can you see it being loaded in Firebug net tab? 您能在Firebug的“网络”标签中看到它吗?

What are your MEDIA values in settings.py? 您在settings.py中的MEDIA值是多少? I have the following and they work fine on the dev server: 我有以下内容,它们在开发服务器上工作正常:

#settings.py
MEDIA_ROOT = 'C:/site/static'
MEDIA_URL = ''

Project structure: 项目结构:

C:/site/
    settings.py
    static/
        javascript/
            script.js
    templates/
    urls.py

In any of your templates: 在您的任何模板中:

<script type="text/javascript" src="/static/javascript/script.js"></script> 

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

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