[GH-ISSUE #605] if set DEBUG = True, run 'python3 manage.py collectstatic --noinput' error. #356

Closed
opened 2026-02-27 15:58:22 +03:00 by kerem · 7 comments
Owner

Originally created by @smallpize on GitHub (Sep 25, 2023).
Original GitHub issue: https://github.com/retspen/webvirtcloud/issues/605

if set DEBUG = True, run 'python3 manage.py collectstatic --noinput' error.

ERROR: django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

something wrong with:

STATIC_URL = "/static/"
if not DEBUG:
STATIC_ROOT = Path.joinpath(BASE_DIR, "static")
else:
STATICFILES_DIRS = [
Path.joinpath(BASE_DIR, "static"),
]

Originally created by @smallpize on GitHub (Sep 25, 2023). Original GitHub issue: https://github.com/retspen/webvirtcloud/issues/605 if set DEBUG = True, run 'python3 manage.py collectstatic --noinput' error. ERROR: django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. something wrong with: STATIC_URL = "/static/" if not DEBUG: STATIC_ROOT = Path.joinpath(BASE_DIR, "static") else: STATICFILES_DIRS = [ Path.joinpath(BASE_DIR, "static"), ]
kerem closed this issue 2026-02-27 15:58:22 +03:00
Author
Owner

@krejcar25 commented on GitHub (Sep 25, 2023):

Hi smallpize,
does using python3 manage.py collectstatic --noinput --settings=webvirtcloud.settings-dev help?

<!-- gh-comment-id:1733566574 --> @krejcar25 commented on GitHub (Sep 25, 2023): Hi smallpize, does using `python3 manage.py collectstatic --noinput --settings=webvirtcloud.settings-dev` help?
Author
Owner

@MisterBlueBear commented on GitHub (Oct 23, 2023):

I am getting the same error message as smallpize. I am using Debian 11 with python 3.9.2
If I use the command "python3 manage.py collectstatic --noinput --settings=webvirtcloud.settings-dev", I get:

Traceback (most recent call last):
File "/srv/webvirtcloud/manage.py", line 21, in
main()
File "/srv/webvirtcloud/manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/core/management/init.py", line 442, in execute_from_command_line
utility.execute()
File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/core/management/init.py", line 416, in execute
django.setup()
File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/apps/config.py", line 193, in create
import_module(entry)
File "/usr/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'debug_toolbar'

<!-- gh-comment-id:1775145772 --> @MisterBlueBear commented on GitHub (Oct 23, 2023): I am getting the same error message as smallpize. I am using Debian 11 with python 3.9.2 If I use the command "python3 manage.py collectstatic --noinput --settings=webvirtcloud.settings-dev", I get: > Traceback (most recent call last): > File "/srv/webvirtcloud/manage.py", line 21, in <module> > main() > File "/srv/webvirtcloud/manage.py", line 17, in main > execute_from_command_line(sys.argv) > File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line > utility.execute() > File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 416, in execute > django.setup() > File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/__init__.py", line 24, in setup > apps.populate(settings.INSTALLED_APPS) > File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate > app_config = AppConfig.create(entry) > File "/srv/webvirtcloud/venv/lib/python3.9/site-packages/django/apps/config.py", line 193, in create > import_module(entry) > File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module > return _bootstrap._gcd_import(name[level:], package, level) > File "<frozen importlib._bootstrap>", line 1030, in _gcd_import > File "<frozen importlib._bootstrap>", line 1007, in _find_and_load > File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked > ModuleNotFoundError: No module named 'debug_toolbar'
Author
Owner

@krejcar25 commented on GitHub (Oct 23, 2023):

Hi, in this case, you are missing some packages. There is a dev requirements.txt file in the dev folder. You'd want to do pip install -r dev/requirements.txt to setup your development environment. This should not be done in production though.

<!-- gh-comment-id:1775742552 --> @krejcar25 commented on GitHub (Oct 23, 2023): Hi, in this case, you are missing some packages. There is a dev [requirements.txt](retspen/webvirtcloud/blob/master/dev/requirements.txt) file in the `dev` folder. You'd want to do `pip install -r dev/requirements.txt` to setup your development environment. This should not be done in production though.
Author
Owner

@krejcar25 commented on GitHub (Oct 23, 2023):

@smallpize If my previous tip worked, please consider closing this issue, thank you!

<!-- gh-comment-id:1775829744 --> @krejcar25 commented on GitHub (Oct 23, 2023): @smallpize If my previous tip worked, please consider closing this issue, thank you!
Author
Owner

@MisterBlueBear commented on GitHub (Oct 26, 2023):

I ran the pip install -r dev/requirements.txt and it fixes the ModuleNotFoundError: No module named 'debug_toolbar' error.
However, when I run python3 manage.py collectstatic --noinput --settings=webvirtcloud.settings-dev, I still get the same error.

django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

Shouldn't STATIC_ROOT be defined regardless if we are using debug or not ?

<!-- gh-comment-id:1781348958 --> @MisterBlueBear commented on GitHub (Oct 26, 2023): I ran the `pip install -r dev/requirements.txt` and it fixes the `ModuleNotFoundError: No module named 'debug_toolbar'` error. However, when I run `python3 manage.py collectstatic --noinput --settings=webvirtcloud.settings-dev`, I still get the same error. > django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Shouldn't STATIC_ROOT be defined regardless if we are using debug or not ?
Author
Owner

@krejcar25 commented on GitHub (Oct 26, 2023):

@MisterBlueBear as you can see in settings.py.template STATIC_ROOT will be set unless DEBUG is enabled in settings.py. Do you perhaps still have that set from before? If you do, please set DEBUG to False in settings.py.

I don't know why it its done this way, maybe @catborise could explain it but that's the way I understand the code now.

<!-- gh-comment-id:1781564192 --> @krejcar25 commented on GitHub (Oct 26, 2023): @MisterBlueBear as you can see in [settings.py.template](https://github.com/retspen/webvirtcloud/blob/9a675918c683d76b621ebd405960fd4c2e3be324/webvirtcloud/settings.py.template#L129) `STATIC_ROOT` will be set unless `DEBUG` is enabled in settings.py. Do you perhaps still have that set from before? If you do, please set `DEBUG` to `False` in settings.py. I don't know why it its done this way, maybe @catborise could explain it but that's the way I understand the code now.
Author
Owner

@MisterBlueBear commented on GitHub (Oct 27, 2023):

When I set Debug to False, there is no issue. However, in this case, I wanted to turn on Debug in order to troubleshoot an issue. Whether Debug is True or False, STATIC_ROOT should be set.

Also, these references state that the static_root should not be in staticfiles_dirs
https://stackoverflow.com/a/27213753
https://stackoverflow.com/a/12161409

Seeing that django.contrib.staticfiles has this check:

staticfiles.E002: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.

And according to the django 4.2 docs for STATIC_ROOT

This should be an initially empty destination directory for collecting your static files from their permanent locations into one directory for ease of deployment; it is not a place to store your static files permanently. You should do that in directories that will be found by staticfiles’s finders, which by default, are 'static/' app sub-directories and any directories you include in STATICFILES_DIRS).

So the static files that are currently in the static_root (i.e. /static) should, by django 4.2 standards, be elsewhere and that other location is defined in STATICFILES_DIRS. In such, this allows collectstatic to collects static files from all /static sub-directories and dirs defined in STATICFILES_DIRS.
Especially if you try to run collectstatic --clear option in order to remove stale static files. As of right now, I would loose all static files. I would use --clear after running in debug in order to remove the static files for debug_toolbar.

All this leads me to believe that current code below is not correct. At least in django 4.2

if not DEBUG:
STATIC_ROOT = Path.joinpath(BASE_DIR, "static")
else:
STATICFILES_DIRS = [
Path.joinpath(BASE_DIR, "static"),
]

Instead, this would be the answer where the static files are stored in webvirtcloud/static/ for example.

STATIC_ROOT = Path.joinpath(BASE_DIR, "static/")
STATICFILES_DIRS = [ Path.joinpath(BASE_DIR, 'webvirtcloud/static/') ]

So whether you are running in debug or not, there is no exception as STATIC_ROOT is always defined.

P.S. I'm not a django expert by any means, so let me know if I am wrong.

<!-- gh-comment-id:1783447441 --> @MisterBlueBear commented on GitHub (Oct 27, 2023): When I set Debug to False, there is no issue. However, in this case, I wanted to turn on Debug in order to troubleshoot an issue. Whether Debug is True or False, `STATIC_ROOT` should be set. Also, these references state that the `static_root` should not be in `staticfiles_dirs` https://stackoverflow.com/a/27213753 https://stackoverflow.com/a/12161409 Seeing that [django.contrib.staticfiles](https://docs.djangoproject.com/en/4.2/ref/checks/#s-staticfiles) has this check: > staticfiles.E002: The [STATICFILES_DIRS](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STATICFILES_DIRS) setting should not contain the [STATIC_ROOT](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STATIC_ROOT) setting. And according to the django 4.2 docs for [STATIC_ROOT](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STATIC_ROOT) > This should be an initially empty destination directory for collecting your static files from their permanent locations into one directory for ease of deployment; it is not a place to store your static files permanently. You should do that in directories that will be found by [staticfiles](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/)’s [finders](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STATICFILES_FINDERS), which by default, are 'static/' app sub-directories and any directories you include in [STATICFILES_DIRS](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STATICFILES_DIRS)). So the static files that are currently in the `static_root` (i.e. `/static`) should, by django 4.2 standards, be elsewhere and that other location is defined in [STATICFILES_DIRS](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STATICFILES_DIRS). In such, this allows `collectstatic` to collects static files from all `/static` sub-directories and dirs defined in STATICFILES_DIRS. Especially if you try to run [collectstatic --clear](https://docs.djangoproject.com/en/4.2/ref/contrib/staticfiles/#cmdoption-collectstatic-clear) option in order to remove stale static files. As of right now, I would loose all static files. I would use `--clear` after running in debug in order to remove the static files for `debug_toolbar`. All this leads me to believe that current code below is not correct. At least in django 4.2 ``` if not DEBUG: STATIC_ROOT = Path.joinpath(BASE_DIR, "static") else: STATICFILES_DIRS = [ Path.joinpath(BASE_DIR, "static"), ] ``` Instead, this would be the answer where the static files are stored in webvirtcloud/static/ for example. ``` STATIC_ROOT = Path.joinpath(BASE_DIR, "static/") STATICFILES_DIRS = [ Path.joinpath(BASE_DIR, 'webvirtcloud/static/') ] ``` So whether you are running in debug or not, there is no exception as STATIC_ROOT is always defined. P.S. I'm not a django expert by any means, so let me know if I am wrong.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/webvirtcloud#356
No description provided.