17π
This was issue with the STATICFILES_DIRS
configuration in the settings.py
file.
This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.
Following was the configuration in my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
Now I updated this code to:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
And every files is loading fine.
Reference Link
13π
Use Whitenoise to make your life easier when dealing with static files in django.
1.If you are using docker-compose,add whitenoise to your requirements.txt file:
whitenoise==3.3.1
2.Add whitenoise to your middleware apps inside settings.py
MIDDLEWARE_CLASSES = [# 'django.middleware.security.SecurityMiddleware','whitenoise.middleware.WhiteNoiseMiddleware',# ...]
make sure that you add this below your security.SecurityMiddleware app
3.Finally, change the following variables inside settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR,'<app_name>/static'),os.path.join(BASE_DIR, 'static'),)
Be sure to replace with the name of your app. Note that this only applies if your static files are stored in(for example) my_project/app/static/app/.
Otherwise if your static folder is located in my_project/app/static:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
-
Lastly disable the built-in django static file server as follows:
INSTALLED_APPS = [ # ... 'whitenoise.runserver_nostatic', 'django.contrib.staticfiles', # ...]
- Select Children of an Object With ForeignKey in Django?
- Django vs. Pylons
- Create a canonical "parent" product in Django Oscar programmatically
0π
As you have moved your project to another directory, there is a possibility that the path of your static directories are also different now. Django in most scenarios use apache, nginx or some other web servers to serve static files
. One point to notice is that your static directory should be accessed publicly. I had gone through a problem like this before. What I did was I moved static dir to document root mentioned in apache config file
.
So move your static files to the doc root of apache and update static directories in settings.py to refer to the static directory in your apache doc root. I hope this helps.
- How can my Model primary key start with a specific number?
- Django order items by two fields, but ignoring them if they're zero
- Django Rest Framework 3.1 breaks pagination.PaginationSerializer