问题
I am relatively new to web dev. and I am trying to build my first web application. I have my static folder in project_root/static but for some reason, I keep getting 404s when I run the server:
Not Found: /users/login/js/bootstrap.min.js for example.
I have {% load staticfiles %} at the top of my html and in my settings.py I have:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Thanks.
EDIT: Fixed with a combination of the answers below. Thank you all!
回答1:
Go ahead and read this- http://agiliq.com/blog/2013/03/serving-static-files-in-django/
Summary: for example if users is the app name in your project, then create dir- 'users/static/js/'. and put bootstrap.min.js. This file will be accessible at localhost:8000/static/js/bootstrap.min.js. Now go ahead and try it out.
So what is happening inside Django? How is it working? read this- http://agiliq.com/blog/2013/03/serving-static-files-in-django/
回答2:
put your static js, css and other files in a directory called static
inside your project directory then these settings will work:
# this defines the url for static files
# eg: base-url.com/static/your-js-file.js
STATIC_URL = '/static/'
# this is directory name where collectstatic files command will put your app level static files
STATIC_ROOT = 'staticfiles'
# this is directory paths where you have to put your project level static files
# you can put multiple folders here
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
based on these settings you will be able to access your static files correctly
来源:https://stackoverflow.com/questions/44089055/django-cannot-find-my-static-files