0

I have a Django project with "django.contrib.staticfiles" in INSTALLED_APPS. Therefore, I can run ./manage.py collectstatic. But I need to use call_command.

Running python -c 'from django.core.management import call_command; call_command("collectstatic")' results in django.core.management.base.CommandError: Unknown command: 'collectstatic'

What am I doing wrong? Shouldn't the 2 commands be absolutely identical? Obviously, environments, paths, etc are correct in both cases. I am running both commands from the same shell in a docker container.

10
  • Do you have the DJANGO_SETTINGS_MODULE environment variable set? Also I see you haven't called django.setup() Commented Jan 19, 2024 at 9:35
  • Does this answer your question? Django Standalone Script Commented Jan 19, 2024 at 9:38
  • Why not DJANGO_SETTINGS_MODULE='myproj.settings' python -m 'django' collectstatic? Commented Jan 19, 2024 at 9:47
  • DJANGO_SETTINGS_MODULE is set. correct me if I'm wrong, but I believe running ./manage.py collectstatic would not work either if it wasn't? Commented Jan 19, 2024 at 10:28
  • 1
    @Gerry no my question is whether you have set the DJANGO_SETTINGS_MODULE environment variable or not. You should set it if you haven't (manage.py has a line that does this usually). Django uses that to find the settings to be used (And hence discovers the apps to be installed). You should also call django.setup() for the same purpose. Commented Jan 19, 2024 at 10:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.