Why doesn't django collectstatic work correctly? - django

When I make collectstatic, it doesn't work, and create static folder and collect into D:\static\.
My settings don't work correctly. Why?
Here are my settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'D:\DjangoProjects\mysite']
STATIC_ROOT = '/static/'

In Python, as in many languages, the backslash is the escape character. If you want a literal backslash, you need to escape it itself:
STATIC_ROOT = 'D:\\static\\'
or you can use the r'' notation for a literal string:
STATIC_ROOT = r'D:\static\'
However, the Django documentation says that you should be using forward slashes, even on Windows. It's not clear whether this also applies to STATIC_ROOT and MEDIA_ROOT, but it is for STATICFILES_DIRS. In that case, STATIC_ROOT would become:
STATIC_ROOT = 'D:/static/'
The 'D:\DjangoProjects\mysite' you have written is certainly an error, should be 'D:/DjangoProjects/mysite'.

Related

Django not updating static css file design in browser when I run collect static command

I wrote css file and then save it all changes happen and when I run collect static command and then I again change css files no changes displayed on browser nothing happen at all.
It may be due to cache problem. So, you can try this on your browser which will reload from start:
Ctrl + R
I would suggest to double check the settings.py if the STATICFILES_DIRS and STATIC_URL is declared there. An example below-
STATIC_URL = '/static/'
STATICFILES_DIRS = [
'static',
]
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
Then create a static folder and media folder where manage.py file is located. Then run the python3 manage.py collectstatic. Hope it works.
It is due to catch problem.
ctrl +f5
Is the solution

Django cache. ManifestStaticFilesStorage

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

Collectstatic referencing to wrong path to folder

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?

path of STATICFILES_DIRS in django

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',]

Django1.3 collectstatic command makes the filename lowercase

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