Django won't refresh staticfiles

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

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

 
相关标签:
15条回答
  • 2020-12-07 19:06

    I was chasing a css static file change tonight. Going thru a Django tutorial. My Django html page would not show the updated css to my base.css file.

    I would see the original base.css but no my update to file.This is on a dev environment.

    I did not think django cached static on dev and only after running coll ecstatic in production.

    For a quick work around I changed the name of my test sites. base.css file to base1.css and the html page link to base1.css

    reloaded page and my updated css took affect. No best solution. but quick fix.

    0 讨论(0)
  • 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 %}
    
    <link rel="stylesheet" href="{% static "css/project.css" %}?{% cache_bust %}"/>
    

    and here is the resulting output:

    <link rel="stylesheet" href="/static/css/project.css?__v__=7d88de4e-7258-11e7-95a7-0242ac130005"/>
    
    0 讨论(0)
  • 2020-12-07 19:10

    One solution is to change the settings to the following:

    STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
    

    What this does is create a copy of the static file with a content hash in the file name (when running collectstatic). This way when the contents are changed the filename is changed and the old cache won't be used. The only problem with this is it doesn't get used when in DEBUG = True mode so you have to do a shift-reload to do a hard reload.

    You can read the docs on ManifestStaticFilesStorage for more info.

    EDIT: I found a solution for making sure static files are not cached in dev and posted it on another question.

    0 讨论(0)
  • 2020-12-07 19:10

    To refresh static files you should run python manage.py collectstatic again.

    0 讨论(0)
  • 2020-12-07 19:11

    Simply running python manage.py collectstatic should do the trick. Then hard refresh browser with ctrl + F5 which should work on all browsers.

    0 讨论(0)
  • 2020-12-07 19:15

    It sounds like both your browsers have the javascript file cached. In Chrome you can clear the cache by pressing Ctrl + Shift + Del and ticking just 'Cached images and files'. Firefox probably has a similar shortcut.

    You can take a look at this question on tips to disable caching of static files on your development server altogether.

    0 讨论(0)
提交回复
热议问题