34👍
What are the STATIC_ROOT
, STATICFILES_FINDERS
, and STATICFILES_DIRS
in your settings.py
?
When collectstatic
is run, the default STATICFILES_FINDERS
value django.contrib.staticfiles.finders.FileSystemFinder
will collect your static files from any paths that you have in STATICFILES_DIRS
.
The other default STATICFILES_FINDERS
value django.contrib.staticfiles.finders.AppDirectoriesFinder
will look in the /static/
folder of any apps in your INSTALLED_APPS
.
All of the static files that are found will be placed in the specified STATIC_ROOT
directory.
Check out this link to the collectstatic docs
And this link an explanation of the various static settings in settings.py
You can also use python manage.py findstatic
to see which directories collectstatic
will look in.
5👍
That just happened to me and I accidentally put the app’s static files directory in the .gitignore file. So on my local machine it got collected just fine, but in production the static files were actually missing (gitignored).
- [Django]-How to create an object for a Django model with a many to many field?
- [Django]-How to override and extend basic Django admin templates?
- [Django]-Kombu.exceptions.EncodeError: User is not JSON serializable
5👍
just do this and make the naming convention same
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
static directory contains all off your project assets
assets directory will create automatic when u run this cmd
python3 manage.py collectstatic
this will copy all static folder content into assets folder
hope this helps 🙂
- [Django]-Machine Learning (tensorflow / sklearn) in Django?
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
- [Django]-Django – what is the difference between render(), render_to_response() and direct_to_template()?
1👍
your are missing the STATIC_ROOT where your static files going to be copied just
add this line in your settings.py
STATIC_ROOT=os.path.join(BASE_DIR,'staticfiles')
your settings.py looks like this :
STATIC_URL = '/static/'
STATIC_ROOT=os.path.join(BASE_DIR,'staticfiles')
#added manully
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
remember to add STAIC_ROOT path in urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path("" ,include("home.urls")),
]+static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
After all of it you can run :
python manage.py collectstatic
it will create a staticfiles folder for you in your Dir
- [Django]-Django or Django Rest Framework
- [Django]-Nested inlines in the Django admin?
- [Django]-How django time zone works with model.field's auto_now_add
0👍
This happened to me because while investigating a bug occurring on certain dates, changed my computer date and time.
Hence it disturbed things like collectstatic, and also my browser history.
Don’t change your computer date and time.
- [Django]-Avoid warnings on 404 during django test runs?
- [Django]-Django 1.8 – what's the difference between migrate and makemigrations?
- [Django]-How to limit django admin inline formsets
0👍
I was under the impression the comparison would be content based. It turned out to be date based. So, don’t mess with your files after collecstatic
.
- [Django]-Per Field Permission in Django REST Framework
- [Django]-Django Broken pipe in Debug mode
- [Django]-Defining Constants in Django
0👍
One Quick work-around, although this does not fix it or explain WHY it’s happening, is to:
- go to your project’s file directory & rename your project’s
‘static’ folder to something else like ‘static-old’ - create a new,empty folder called ‘static’ in your project directory
- now if you run
python manage.py collectstatic
it will see that nothing is
in your static folder and will copy ALL static files over.
- [Django]-Configuring Django to use SQLAlchemy
- [Django]-Django: how to create custom "base" model
- [Django]-Django whitenoise drawback
0👍
if you have setup everything properly for static and you are using nginx
then enter this command
sudo nginx -t
You will see error
why your static files aren’t being served and fix that specific error
In my case I gave wrong path in my nginx config
location /static {
root /home/ubuntu/myproject/app/static/;
}
- [Django]-Creating a extended user profile
- [Django]-Request.data in DRF vs request.body in Django
- [Django]-What's the difference between CharField and TextField in Django?