I do not know why Django created a folder named staticfiles rather than static as expected. That might be the reason why I got an error after running python manage.py collectstatic:
The system cannot find the path specified: 'D:...\\static'
My settings file included:
from pathlib import Path
import os
import django_heroku
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
I had already tried to change 'staticfiles' to 'static' in STATIC_ROOT, but no success.
Could anyone please explain why and how to create a folder name "static"?
Thank you very much in advance!
I considered your static files are placed inside your app(s)
Try removing STATICFILES_DIRS so the setting will be:
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
...and try to re-run python manage.py collectstatic
Here is how collectstatic works:
First it collect static files from STATICFILES_DIRS if any, if there is no STATICFILES_DIRS in settings it collects static files from each app
Then placed them to STATIC_ROOT, so if your static files are placed inside your app(s) better to remove STATICFILES_DIRS
If still there is an error share your project structure
Related
I've created a django project with some apps. Initially, I just had a single app and i had static files and templates in it. As my project grew, I added some other apps to the project which they are still accessing the statics files and templates from inside the main app. I wasn't giving much care to this problem until i tried to make a simple production and used collectstatic command. Seems i can collect my static files to the STATIC_ROOT directory but i can't access them in the templates. Here is my project structure:
shop/
-myshop(main app)
--statics
--templates
-(some other apps beside main app)
-shop(directory created by project containing settings.py, manage.py)
This is my relevant settings.py configurations:
BASE_DIR = Path(__file__).resolve().parent.parent
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
...
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'myshop/static',
]
STATIC_ROOT = BASE_DIR / 'static'
After running collectstatic django creates the static directory in root of project but when i remove or rename the myshop/static i get static files 404 in runserver.
I dont know how to check {% load static %} resulting path in runtime to put more information here. Is there any debug routine to understand what's django backend doing?
Try this
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
STATIC_ROOT = BASE_DIR.parent / "static_cdn"
$ python manage.py collectstatic This command will copy all files from your static folders into the STATIC_ROOT directory.
You must write the names of the static folder and the static root folder diffrent.
To learn more https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files
I tried deploying Django project to www.pythonanywhere.com, My website ran but could not load static files.
I did collectstatic command in hosting server console but got error.
my project file configuration as follows
myproject # parent directory
-DjangoApp1 # an app inside project
-Myproject main file(includes manage.py etc)
-DjangoApp2 # an app inside project
-DjangoApp3 (in this app my static files located `DjangoApp3/static/DjangoApp3` )
in settings.py file
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = ('/DjangoApp3/static/DjangoApp3/',)
STATIC_URL = '/static/'
when I run collectstatic method in local machine ,below error occured
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\DjangoApp3\\static'
This works
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'staticfiles'),)
STATIC_URL = '/static/'
I've covered the three in an answer here before
Try this:
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
(os.path.join(BASE_DIR, 'static')),
)
"python manage.py collectstatic" is not working, I think it might not work for any file and this fa-brands-400.eot happened to be the first file.
Error:
django.core.exceptions.SuspiciousFileOperation: The joined path (/Users/monica/music-emotion/static/webfonts/fa-brands-400.eot) is located outside of the base path component (/Users/monica/music-emotion/staticfiles)
Is settings.py supposed to be located in root or can it be in a subdirectory?
My settings.py has this at the end-
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATICFILES_DIRS = (
os.path.join('me_main/static'),
)
STATIC_URL = '/static/'
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())
I had the same error I just removed
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
and it works fine now.
I'm setting up my static files for Django to collect with this setup,
STATIC_URL = '/static/'
STATIC_ROOT = "127.0.0.1:8000/static/"
I made a static folder inside my app
my_app/static/my_app/my_css_framework_directory
My my_css_framework_directory contains the following directories, css, img, and js.
But when I ran python manage.py collectstatic, 0 files are copied?
STATIC_ROOT should be the absolute path to your static directory https://docs.djangoproject.com/el/1.10/ref/settings/#std:setting-STATIC_ROOT
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
I'm having trouble showing my CSS/Static files on my django app I just deployed on Heroku. It doesn't show any debugging errors, so I don't know where to start.
Here is my code:
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
STATIC_PATH = os.path.join(PROJECT_PATH, 'static')
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
STATIC_PATH,
)
Used WhiteNoise to collectstaticfiles. Thanks for helping.
https://devcenter.heroku.com/articles/django-assets for documentation.
Try setting an absolute path to your STATIC_ROOT. From Django documentation: Settings:
STATIC_ROOT
The absolute path to the directory where collectstatic will collect static files for deployment.
Then make sure that you run collectstatic.
Last, I usually set STATICFILES_DIRS to None for deployment.