I need to refresh a js file in my static files folder, however whenever I run python manage.py collectstatic, a clone of my static files folder is created in a diffrent part of the project, and the js file is still not updated. I tried changing the STATIC_ROOT variable in the settings.py file to the actual location of my static folder, but it doesn't seem to refresh because the collectstatic warning message in the console says its going to be saved in a completely different location.
The Structure of my project is
resume_V6-test
-resume
-home
-static (actual location)
-resume
-settings.py
-store
-staticfiles (clone of the static folder)
-users
and my settings.py is
# Commented out the location it was before I tried fixing the error
#STATICFILES_DIRS = [
#os.path.join(BASE_DIR, "static"),
#'/resume_V6-test/resume/home/static',
#]
#STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_ROOT = os.path.join(BASE_DIR, 'home/static')
STATIC_URL = '/static/'
And the error message is
python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
C:\resume_V6-test\resume\staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Thanks
STATIC_ROOT is the output destination for the static file. E.g. this is where collectstatic collects all the static files to (read here https://docs.djangoproject.com/en/3.0/howto/static-files/)
The default is to collect all static files from all apps from /static directory of each app.
If you want extra location to collect from, you can use the STATICFILES_DIRS settings. (https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-STATICFILES_DIRS)
Related
I tried loading style.css in static file but it wouldn't work. I made sure to configure STATIC files in settings.py like this
STATIC_URL = '/static/'
STATICFILES_DIR = [str(BASE_DIR.joinpath('static'))]
All my paths are correct and I loaded static. I cleared my cookies and the CSS still wouldn't work.
if you are running this on django's built-in server then one thing you can do is to make sure your static files folder is in the same directory as the manage.py file because when we run the server as python manage.py runserver the relative path for everything else starts from the directory where manage.py is
I have a Django server running with nginx and gunicorn.
All static files are served correctly, only the admin page has no css, js, etc. .
I already ran collectstatic and restarted nginx and gunicorn.
Nothing changed.
What can I do?
Make sure to mention the locations of your staticfiles_dirs in settings.py. Also remember to mention the locations of your static files in a dynamic format instead of hard coding them in your html document.And make sure to load static in base.html.
Try using this sample code in your settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'templates'),
'/var/www/static/'
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
After this use the following command in terminal:
python manage.py collectstatic
This should create the static folder and copy all the static objects inside
I have a Django live version in production and I develop in local with Django manager.py.
I'm not able to find the correct configuration settings for my static files.
All my static files are on a /static/ on the root of the project, because all the sub-apps use the same stylesheets and so on.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
On my live version : it works because I've got an NGINX rule that allow access to /static/ directly.
But when I want to work on local, the static files are 404, with Debug True and False.
I don't see the interest for my needings of the staticfiles directory (but I can have a wrong mind here).
Is it possible to have all the static files in subdirectories in /static/ (css, img, js, etc.) and to set a workable settings on both local and live production without switching something in settings.py for deployment ?
Thanks for your help :)
I've found the solution :
After moving my static directory from the root directory of Django the folder of my main app, I made a collectstatic with that settings :
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
To serve the static files (CSS, JS), I set the settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
This means that all my static files need to be in the project root's static folder
/app1
/app2
/media
/static #They need to be stored here, all static files
/templates
urls.py
settings.py
manage.py
However, I am storing them in my app's static folder.
/app1/static/ # Storing the static files here
/app2/static/ #and here
/media/
/static/ # but not here
Still, Django is able to serve them, how is that possible?
I tried the same thing with media files; setting
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and storing the media (images etc) files in each app's individual media dir. This time, Django did not serve the files and served them only when either I moved the files to the project root's media dir or changed the setting to MEDIA_ROOT = os.path.join(BASE_DIR, '<app_name>/media')
Why was I allowed to serve static files even when they weren't in the project root static dir but not the media files - they were only served from the root media dir.
Please read the documentation carefully - https://docs.djangoproject.com/en/1.9/ref/settings/#staticfiles-finders
It precisely states that django will search for static files in each app + the directory stated in settings.
Quoting as in the documentation -
The default will find files stored in the STATICFILES_DIRS setting (using django.contrib.staticfiles.finders.FileSystemFinder) and in a static subdirectory of each app (using django.contrib.staticfiles.finders.AppDirectoriesFinder). If multiple files with the same name are present, the first file that is found will be used.
While this is not true for media files. Django doesn't look for media files in subdirectories.
i have in my settings.py used
STATIC_ROOT = '/static/'
to collect the static files
this is my static_path settings
STATIC_PATH = os.path.join(BASE_DIR,'static')
when i run the collect static code django makes a static folder outside my workspace folder,should't it collect all the static files in the static folder which i have already created?
I think you've confused the STATIC_ROOT and STATIC_URL and STATICFILES_DIRS setttings.
To tell Django to serve your static files from example.com/static/, set
STATIC_URL = '/static/'
You shouldn't set STATIC_ROOT = '/static'. This is telling Django to collect your static files in /static/ on your disk, which is outside of your workspace.
STATIC_ROOT is the directory where the static files will be collected. It should be something like
STATIC_ROOT = '/var/www/example.com/staticfiles/'
If you don't want to hardcode the directory, you could do something like:
STATIC_ROOT = 'os.path.join(BASE_DIR, "staticfiles"),'
You would then configure your webserver (e.g. Nginx or Apache) to serve the files in the STATIC_ROOT.
Finally, STATICFILES_DIRS is a list of locations that Django will search for static files when you run collectstatic. The files will be copied from that directory to your STATIC_ROOT. Therefore you need to make sure that the STATIC_ROOT is not included in STATICFILES_DIRS.
Basically you can create multiple 'static' folders for different applications of Django project and those can be used in development. But whenever you go to production servers and to serve the all static files from the same place then you need this functionality in which you can collect all static files from different directory to one directory out of your workplace.
project_name
- project_name
- static
- static files goes here which are common
- __init__.py
- settings.py
- urls.py
- application_name
- static
- static files goes here which are related to application only
- __init__.py
- settings.py
- urls.py
- static - This is common place where all static files will be placed after collectstatic command for productions only.
According above structure you can understand you can server static files using different server like CDN.
Let me know if you are not satisfied with answer. Thanks.