Django development cannot find third party script

2019-08-02 07:27发布

问题:

I'm working in a development environment with debug = True on Windows 10.

I have installed node and npm and run npm microm from the main folder of my project. It created a node_modules subfolder with a microm folder inside, among others. so the microm.js script is located in:

f:\roomtemp\node_modules\microm\microm.js

The relevant portion of my Settings file is:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'node_modules')

My template contains the following:

{% load staticfiles %}
<script src='{% static "/microm/microm.js" %}'></script>

When I attempt to load the page, the Chrome console shows a 404 and I see this in the server console:

"GET /static/microm/microm.js HTTP/1.1" 404 1661

I have read many SO questions that seem to contain the solution but none have worked so far. I've also searched high and wide on the internet. No luck so far. Any help would be much appreciated.

Mike

回答1:

First of all I don't think you should STATIC_ROOT at your node_modules folder. STATIC_ROOT is where the django management commant collectstatic will put all the static files from all the apps into when you are readying for deploying. This process may result in some files being overwritten.

Please restore STATIC_URL to some sane value like

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')

Then use the STATICFILES_DIRS setting. This is what tells django what extra locations to look for static files.

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'node_modules')]

Finally, make sure that your STATICFILES_FINDERS has these

'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',


标签: django npm