When executing the command 'collectstatic', python looks into the wrong folder, Dev/staticfiles, when my staticfiles are saved in the folder: [..]/Dev/bidding_tool_project/project/staticfiles
Would you have any idea how/why this happens?
An other SO answer helped: os.path.join considers anything after a slash as being an absolute path. When using os.path.join, the path should be declared as: STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') and not STATIC_ROOT = os.path.join(BASE_DIR, 'project/staticfiles')
the post that helped me: Why doesn't os.path.join() work in this case?
Related
I have just started to use ManifestStaticFilesStorage and I am running into a problem I do not understand.
Firstly, if I prepare and run the website without ManifestStaticFilesStorage, then everything displays properly. Within the browser I can for example, see a link to an image and if I follow that it shows the image in a resource box:
Next, I add the following line of code into settings
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
and I also have:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
I then delete the static directory and rebuild everything (migrations, collect static).
This produces a website with the following link to an image, but the resource is not visible:
However, when I go into the static folder in my project and look at the directory, I can see the file clearly and as far as I can tell, it appears normal:
It's like Django is failing to link up correctly when I use ManifestStaticFilesStorage.
I have included some other settings that may be relevant:
STATIC_URL = '/static/'
# STATIC_ROOT = 'static'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
'./ebdjango/static/',
]
I'm new to this, so if you have any suggestions on how to proceed with the cache in Django, please let me know.
Many thanks
Mark
I have a project that has been running just fine for about 6 months. Static files have been working perfectly, and everything is great. I have my static files located in a folder as so:
/var/www/html/static/
In my settings.py file, I have the static section setup like so:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/html/static/',
)
This has been working just fine.
However, I now want to move the static folder to a different location. Specifically, I want to move it inside the main project directory. My project is located at /var/www/html/shq/ so I want to have my static directory located at /var/www/html/shq/static/. I moved the folder, then updated my settings.py file to look like this:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/html/shq/static/',
)
However, it didn't work. The Django project is still referencing the old location.
What am I missing here? Why isn't the Django project using the new location of /var/www/html/shq/static/?
EDIT
This is what the tail end of my settings.py file looks like:
119 STATICFILES_FINDERS = [
120 'django.contrib.staticfiles.finders.FileSystemFinder',
121 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
122 ]
123
124 STATIC_URL = '/static/'
125 STATIC_ROOT = '/var/www/html/collected_static/'
126 MEDIA_URL = '/media/'
127 MEDIA_ROOT = '/var/www/html/shq/media/'
128 STATICFILES_DIRS = [
129 os.path.join(BASE_DIR, "static"),
130 '/var/www/html/shq/static/',
131 ]
You might try doing something like this. I think it returns the list of directories that django looks for to find static files. Might help debugging.
from django.contrib.staticfiles import finders
from pprint import pprint
pprint(finders.find("", all=True))
Also, I may not be fully understanding your scenario, but you might confirm that the STATIC_ROOT is set to the location where you want to serve your static files (where your webserver will serve the files). The STATIC_DIRS setting tells collectstatic where to find static files, but the STATIC_ROOT is where collectstatic will actually place the files.
I figured it out. Not surprisingly, it was an easy fix once I figured it out.
It had nothing to do with my Django settings and everything to do with Apache.
Original
Alias /static/ /var/www/html/static/
So no matter what I did in my Django settings.py file, Apache was overriding that to send /static/ requested to the wrong directory.
New Apache Setting
Alias /static/ /var/www/html/shq/static/
Now the proper static files are being referenced. Hopefully this helps someone else in the future :)
I have a MyApp/static/MyApp directory.
When I run ./manage.py collectstatic, I expect the MyApp directory be copied to STATIC_ROOT, but it doesn't.
I have DownloadedApp/static/DownloadedApp as well and its copied to STATIC_ROOT fine.
What am I missing?
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.
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 :)
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).
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
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.
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.
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.
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/;
}
I am from PHP background and want to give path to
STATICFILES_DIRS
in settings.py and in windows I understand that I will need to give full path like:
D:/path/to/folder/project/static
I want to know that is there a way to give some path like "/static/" or can we give path like dirname(__FILE__) in PHP?
So that my path is not hardcoded. Also why it is physical path as webserver normally don't follow physical path for loading CSS files as it use http path? So why it is this way(physical path) in django? Can some one please explain.
thanks for everyone's effort in advance.
In my settings.py files, I always do
import os
ROOT_PATH = os.path.dirname(__file__)
Then you can do
STATICFILES_DIRS = [os.path.join(ROOT_PATH, 'static')]
The reason that STATICFILES_DIRS wants a filesystem path is that it needs to know where the files live in the operating system (so things like manage.py collectstatic are possible). The STATIC_URL setting is for webserver paths, and that's what it uses to show to the user in the admin or the {% static %} tag or whatever. STATICFILES_DIRS is server-side only and never shows up in any rendered templates or anything.
Find the settings.py file in the project,
Put:
STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'))
Change to:
STATICFILES_DIRS=[(os.path.join(BASE_DIR,'static'))]
Answer on your question is wrong. If you do like this, you'll get the next error:
ImproperlyConfigured: Your STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma?
My decision is add to top of settings.py file the next function:
import os
def look_folder_tree(root):
result = ()
for dir_name, sub_dirs, file_names in os.walk(root):
for sub_dir_name in sub_dirs:
result += (os.path.join(dir_name, sub_dir_name),)
return result
# Django settings for project.
PROJECT_DIR = os.path.dirname(__file__)
and call look_folder_tree function at:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = look_folder_tree(STATIC_ROOT)
My decision is allow to add all sub folders inside static directory.
In settings.py :
Import os (already done when you create a django project)
import os
You can just use STATICFILES_DIRS as a tuple, mind the trailing comma.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Or you can use the STATICFILES_DIRS as a list data-type, comma at the end is not required here:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
Change this:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'))
To This:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Add a comma at last as,
ERRORS:
?: (staticfiles.E001) The STATICFILES_DIRS setting is not a tuple or list.
HINT: Perhaps you forgot a trailing comma?
your syntax is correct, I think you forget of importing os at the first line of code as import os.
If you are using newer versions of Django i.e Django 3.1, and your BASE_DIR is like this ...
BASE_DIR = Path(__file__).resolve().parent.parent
Then do this :-
STATICFILES_DIRS =[BASE_DIR / 'my_app/static',]
I'm using the Django1.3 and python2.7.1.
The collectstatic is a new feature in Django1.3,so i try to use it in my project.
After setup the STATICFILES_DIRS and STATIC_ROOT, STATIC_URL to include each app static directory,
STATIC_ROOT = os.path.join(ROOT_DIR, 'static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(ROOT_DIR, 'common/static/'),
os.path.join(ROOT_DIR, 'apps/brokee/static/'),
os.path.join(ROOT_DIR, 'apps/home/static/')
)
i run the command:
python manage.py collectstatic
The static files are copied to the /static/ folder,but I found that all javascript file's name become lowercase.I try it several times and the same result got.
I wonder if it is a bug or something I don't know.
See this ticket https://code.djangoproject.com/ticket/15636