Django won't refresh staticfiles

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

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

 
15条回答
  •  醉梦人生
    2020-12-07 18:57

    I have also struggled with this problem for hours. I have tried to inject random string using Javascript, but this method seems stupid and ugly-looking. One possible way to handle this problem is to introduce a custom tag. See this document for details:

    Specifically, you need to create a package called templatetags in whatever apps you have created (or create a new one if you want). And you create any file in this package, and write something like this:

    from django import template
    from django.utils.crypto import get_random_string
    from django.templatetags import static
    
    register = template.Library()
    
    
    class StaticExtraNode(static.StaticNode):
    
        def render(self, context):
            return super().render(context) + '?v=' + get_random_string(32)
    
    
    @register.tag('static_no_cache')
    def do_static_extra(parser, token):
        return StaticExtraNode.handle_token(parser, token)
    
    
    def static_extra(path):
        return StaticExtraNode.handle_simple(path)
    

    then you can use tag {% static_no_cache '.../.../path...' %} to create a path with random arguments!

    I hope this would help!

提交回复
热议问题