Search code examples
djangostatic-files

Force Django ManifestStaticFilesStorage to always return urls with hashes


By default, ManifestStaticFilesStorage only returns urls with hashes in them when DEBUG=False. I want my development environment to be as close to production as possible, but I do need to set debug to False for a couple of things during development. Is there a way to tell ManifestStaticFilesStorage to always give me the url with hash?


Solution

  • ben432rew's answer is close, but it's imperative that the super call of url passes name rather than hashed_name, unless you add some other code to the method that produces a variable with that name. Additionally, it's important that the last line of the your storage class's url return the value from its super:

    from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
    from django.conf import settings
    
    
    class BigBlindManifestStaticFilesStorage(ManifestStaticFilesStorage):
    
        def url(self, name, force=True):
            """
            Override .url to use hashed url in development
            """
            return super(ManifestStaticFilesStorage, self).url(name, True)