Django won't refresh staticfiles

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

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

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

    "Changes of staticfiles is not working". There can be multiple reasons for that.

    1. Your browser is storing cache of your static files.

      Solution (1): Open Incognito/Private window

      Solution (2): Use hard refresh:

      • For mac: cmd + shift + r
      • For others: ctr + shift + r
    2. If you're facing this issue in production then follow the steps below:

      • Remove staticfiles folder by following command: sudo rm -rf staticfiles [inside your project directory]

      • Now run collectstatic command: python3 manage.py collectstatic

      • Restart gunicorn, by following command: sudo systemctl restart gunicorn

      • Restart nginx, by following command: sudo systemctl restart nginx

    Sometimes browser stores thes staticfiles data (as caches) for a certain time. After couple of hours the problem may gone.

    Hope this will fix you issue.

    0 讨论(0)
  • 2020-12-07 19:18
    • Clearing static file python manage.py collectstatic --noinput --clear. This will clear the statics beforehand.

    • Clear the browser cache

    • Add a random string after the js file include, e.g jquery.js?rand=23423423, with each load.

    Did it help?

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

    Instead of using complicated solutions you can add extra parameter to your includes in the templates.

    For static includes:

    <script src="{% static 'js/polls/polls.js' %}?version=1"></script>
    

    For direct includes:

    <link rel="stylesheet" type="text/css" href="/site_media/css/style.css?version=1" />  
    

    Note the ?version=1 in the code. Every time you're modifying the css/js file, change this version in the template, so browser will be forced to reload the file.

    And if you want to avoid caching at all for some unknown reason, you may use the current timestamp instead of version:

    <link rel="stylesheet" type="text/css" href="/site_media/css/style.css?{% now "U" %}" />
    
    0 讨论(0)
提交回复
热议问题