Django pathing to javascript doesn't work

六眼飞鱼酱① 提交于 2019-12-13 04:12:44

问题


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.

Directory structure is

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

In index.html I have

<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.


回答1:


How about:

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

Can you see it being loaded in Firebug net tab?




回答2:


Django does not serve static assets by default. It is possible to make it do it, in the development environment only - see the documentation.




回答3:


What are your MEDIA values in settings.py? 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> 


来源:https://stackoverflow.com/questions/1336736/django-pathing-to-javascript-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!