Django pathing to javascript doesn't work

2019-08-18 12:15发布

问题:

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>