romanvm / django-tinymce4-lite

TinyMCE 4 editor widget for Django
MIT License
126 stars 47 forks source link

ValueError when running Django management commands with ManifestStaticFilesStorage #17

Closed romanvm closed 6 years ago

romanvm commented 6 years ago

When ManifestStaticFilesStorage is used as a storage class for static files, running collectstatic any Django management command with DEBUG = False causes ValueError exception because staticfiles_storage.url() tries to get hash-versioned filename from manifest.json that is not there yet. Current workaround is to temporarily switch to DEBUG = True and run collectstatic but it is not a good idea to manipulate with DEBUG in production. A solution is needed that both allows to run collectstatic with DEBUG = False and retains serving hash-versioned static assets from tinymce4-lite.

AlJohri commented 6 years ago

@romanvm This fix unfortunately isn't completely working for me.

if JS_URL is None:
    # Ugly hack that allows to run collectstatic command with ManifestStaticFilesStorage
    _orig_debug = settings.DEBUG
    if 'collectstatic' in sys.argv:
        settings.DEBUG = True
    JS_URL = staticfiles_storage.url('tinymce/js/tinymce/tinymce.min.js')
    settings.DEBUG = _orig_debug

I have a bunch of other commands using production environment variables, but locally. For example, my script to refresh the database now fails:

#!/bin/sh

set -e

envdir envs/prod pipenv run django-admin pg_dump && \
envdir envs/local pipenv run django-admin dropdb --noinput # allow dropdb to fail locally

envdir envs/local pipenv run django-admin createdb --noinput && \
envdir envs/local pipenv run django-admin pg_restore && \
envdir envs/local pipenv run django-admin migrate # runs local migrations on top of last version of prod db

The envdir envs/prod pipenv run django-admin pg_dump command fails.

romanvm commented 6 years ago

Is this the same issue with ValueError and ManifestStaticFilesStorage?

AlJohri commented 6 years ago

@romanvm yeah, I get the ValueError since 'collectstatic' in sys.argv doesn't apply. I'm running a different command so collectstatic isn't in the sys.argv. Seems like the code gets run on import.

romanvm commented 6 years ago

I suspected that it may not work with other management commands that import the whole Django project. Currently I don't have an acceptable solution so any help is appreciated.

Is there any specific reason why you are using ManifestStaticFilesStorage. Maybe the default file storage class will be enough?