Django won't refresh staticfiles

前端 未结 15 1122
渐次进展
渐次进展 2020-12-07 18:31

This is annoying. I have a javascript file referenced on a django template:

 
15条回答
  •  萌比男神i
    2020-12-07 19:07

    You need to bust the browser cache. This template tag will output a time based uuid when DEBUG=True. Otherwise it will look for a PROJECT_VERSION environment variable. If that is not found it will output a static version number.

    import os
    import uuid
    from django import template                                                                                                              
    from django.conf import settings                                                                                                         
    
    register = template.Library()                                                                                                            
    
    @register.simple_tag(name='cache_bust')                                                                                                  
    def cache_bust():                                                                                                                        
    
        if settings.DEBUG:                                                                                                                   
            version = uuid.uuid1()                                                                                                           
        else:                                                                                                                                
            version = os.environ.get('PROJECT_VERSION')                                                                                       
            if version is None:                                                                                                              
                version = '1'                                                                                                                
    
        return '__v__={version}'.format(version=version)
    

    You would use in a template like this:

    {% load cache_bust %}
    
    
    

    and here is the resulting output:

    
    

提交回复
热议问题