retspen / webvirtcloud

WebVirtCloud is virtualization web interface for admins and users
1.63k stars 362 forks source link

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

Closed smallpize closed 8 months ago

smallpize commented 9 months ago

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"), ]

krejcar25 commented 9 months ago

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

MisterBlueBear commented 8 months ago

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'

krejcar25 commented 8 months ago

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.

krejcar25 commented 8 months ago

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

MisterBlueBear commented 8 months ago

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 ?

krejcar25 commented 8 months ago

@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.

MisterBlueBear commented 8 months ago

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.