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
Related
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)
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 web application runing, the statics files from apps works but in /admin/ seems like doesn't have a css file. In the production the admin page works fine.
My question is where is the css file from admin page of django?
Just in case if somebody want to check, this is my configuration of static files of the web-app in deployment
STATIC_URL = '/static/'
STATIC_ROOT = '/apps_wsgi/generic_name/main/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = '/apps_wsgi/generic_name/main/media/'
You need to run the collectstatic management command. There are static files for other apps in the site-packages of your virtualenv and that command will copy/generate them into the correct static files location for your application.
I'm getting 404 for static files when DEBUG=False in my settings. The file is served fine when DEBUG=True.
I'm looking at the docs and all my configs seem OK to me.
# my_settings.py
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/`
then I run collectstatic --settings=my_settings, and ls static to confirm it has collected the files. It has:
ls ls static/css/core.css
# (its there).
But when we request "localhost:8000/static/css/core.css" we get 404.
Note running python manage.py findstatic css/core.css --settings=my_settings fails to find the file. When I do DEBUG=True and run findstatic it finds the file, but in the sub app's static dir, not the collected directory (STATIC_ROOT).
Note I have:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
and "staticfiles" is in my INSTALLED_APPS.
Django 1.6
As you have specified DEBUG = FALSE in settings, its now left on HTTP server like nginx or apache to serve the static files. You need to configure your webserver to serve static files. An example for apache can be found here.
This si my settings.py about static in settings.py:
STATIC_ROOT = '/home/coat/www/site/app/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"/usr/lib/python2.6/site-packages/django/contrib/admin/static/",
# This is Django admin default static files
)
I user django server:
./manager runserver
Then I open the URL: http://localhost:8000/static/admin/css/base.css
It works very well.
But a open http://localhost/static/admin/css/base.css
It print '404'
I had restart Nginx and uwsgi for many times, but it dosen't works.
First things first, this is nonono:
STATIC_ROOT = '/home/coat/www/site/app/static/'
Never hardcode absolute paths, you're just making your settings file less portable and probably killing kittens. Adapt this to your needs:
import os.path
import posixpath
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# fix STATICFILES_DIRS too
Now, to your question. django.contrib.staticfiles is fantastic but probably a little confusing at first.
You must understand the collectstatic command:
Collects the static files into STATIC_ROOT. [...] Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.
With runserver, staticfiles are served automatically, but in production mode (DEBUG=False, real HTTP server like Nginx), you should run collectstatic to (re)build STATIC_ROOT
STATIC_ROOT: is the root path where the HTTP server should serve static files from.
STATIC_URL: is the root URL where the HTTP server should serve static files to.
STATICFILES_DIRS: other static directories, in addition to each app's "static" subdirectory. Because django.contrib.admin is a normal app with a "static" folder, there is no need to specify it in the settings.
Conclusion: if STATIC_ROOT resolves to /home/coat/www/site/app/static/, and STATIC_URL is /static/, then you should:
Run collectstatic management command
Configure Nginx to serve /home/coat/www/site/app/static/ on /static/, ie.:
location ^~ /static/ {
alias /home/coat/www/site/app/static/;
}
Reload nginx
In development the runserver command does some magic to serve your static files. In production you need to configure NGinx to do it. There are two chapters in the Django documentation I recommend you read:
Serving static files in production
Serving files (Deployment)
Especially the last link explains how you have to configure your NGinx to serve your static files.