Django - Is My Static File in the Wrong Place?

825 Views Asked by At

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')

enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

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.