简体   繁体   中英

Django - Is My Static File in the Wrong Place?

I'm creating a project for a weather app with Django, and I think my Javascript file is in the wrong place. I have it in a static folder. But I'm getting the console error GET http://127.0.0.1:8000/static/capstone/capstone.js net::ERR_ABORTED 404 (Not Found)

Here is how my project files are set up. Is this correct?

In settings.py I also have:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

在此处输入图像描述

make a new folder capstone and move your capstone.js to it.
cause by default Django uses a static folder within an app, so in your case, you're visiting http://localhost:8000/static/capstone/capstone.js but the actual link is http://localhost:8000/static/capstone.js

Your BASE_DIR by default points to the same directory that manage.py is in. If you haven't changed it then capstone.js is currently located in

os.path.join(BASE_DIR, 'capstone/static')

BUT django by default looks for a static folder inside each installed app so in your case STATCFILES_DIRS is redundant.

Make sure you have added the static files to your urls.py .

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

or

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    # ...urls
]

# static content urls
urlpatterns += staticfiles_urlpatterns()

This is my best guess as to why you are getting a 404 error.

NB: if you have setup static urls then try

http://127.0.0.1:8000/static/capstone.js instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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