简体   繁体   English

Django静态文件有时加载,有时不加载

[英]Django Static Files sometimes load, sometimes not load

I am developing a django app in which i have made a static folder in app folder.我正在开发一个 django 应用程序,其中我在 app 文件夹中创建了一个静态文件夹。 I am encountering a problem related to static file.我遇到了与静态文件相关的问题。 The problem is that while i run the app by runserver command, django does collect static files but when i change something in those static files sometime file gets updated and sometime does not get updated and instead it collects old version of those files even if i save the file and run the app again by runserver command.问题是,当我通过 runserver 命令运行应用程序时,django 确实会收集静态文件,但是当我更改这些静态文件中的某些内容时,有时文件会更新,有时不会更新,即使我保存,它也会收集这些文件的旧版本文件并通过 runserver 命令再次运行应用程序。 When I rename the static files (both css and js files) and change the index.html as per new names, the app works totally fine with updated file names for a while and i face this problem again and again.当我重命名静态文件(css 和 js 文件)并根据新名称更改 index.html 时,该应用程序在更新文件名的情况下完全正常工作了一段时间,我一次又一次地面临这个问题。 I am tired of renaming the files and not got any proper solution to this problem.我厌倦了重命名文件并且没有得到任何适当的解决方案。 I will be thankful for your opinion and help.我将感谢您的意见和帮助。

The folders of my app look like this:我的应用程序的文件夹如下所示:

├───.vscode
├───Project
    └───manage.py    
    └───Project
        └───__pycache__/
        └───__init__.py
        └───asgi.py
        └───setting.py
        └───urls.py
        └───wsgi.py
    ├───templates
    │   └───base.html
    └───AppName
        └───templates
            └───index.html 
        └───static
            └───AppName
                └───js
                    └───indexjs.js //--> This file gets loaded with updated version and sometimes not
                └───css
                    └───style.css  //--> This file gets loaded with updated version and sometimes not
        └───views.py
        └───models.py
        └───urls.py
              .
              .
              .
              .
   

static files setting in settings.py settings.py 中的静态文件设置

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'AppName/static')]

static file settings in my index.html file looks like :我的 index.html 文件中的静态文件设置如下所示:

{% extends 'base.html '%}
{% load static %}

{% block content-style-1 %}

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">



<script src="{% static 'AppName/js/jquery-3.5.1.min.js' %}"></script>
<link rel="stylesheet" href="{% static 'AppName/css/style.css' %}">

<script src="{% static 'AppName/js/indexjs.js' %}"></script>



{% endblock %}

{% block content-1 %}

<div class="main">
    <p>To do App Django-1</p>
</div>

<div class="main-container-1"> 
        <input type="text" id="textbox-1" name="name" width="50px">
        <button type="button" id ="btn-2" class="btn btn-primary">Create</button>
</div>

<div class="card mb-1" id="task-card-id">
    <div class="card-body"><p>Card Content</p><button type="button" id="btn-3" class="close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
</div>


{% endblock %}

Sometimes, the CSS doesn't update because it is saved in the cache.有时,CSS 不会更新,因为它保存在缓存中。 When you edit the CSS, try to clear cache by doing (if you are on google chrome):当您编辑 CSS 时,请尝试通过执行以下操作来清除缓存(如果您使用的是 google chrome):

At the top right, click More .点击右上角的更多 。

Click More tools.单击更多工具。 Clear browsing data.清除浏览数据。

Next to "Cookies and other site data" and "Cached images and files," check the boxes.选中“Cookie 和其他站点数据”和“缓存的图像和文件”旁边的复选框。

Click Clear data.单击清除数据。

Sometimes you have to hard refresh the browser to see the changes, especially if it's Chrome.有时您必须硬刷新浏览器才能看到更改,尤其是在 Chrome 中。 Try deleting the cookies as well to see if the problem stops.尝试删除 cookie 以查看问题是否停止。 What I do sometimes is open the same page in another browser for direct comparison.我有时会在另一个浏览器中打开同一页面进行直接比较。

You can hard refresh by holding down ctrl and clicking the reload icon.您可以通过按住ctrl并单击重新加载图标来硬刷新。

What do you mean by "file gets updated" ? “文件已更新”是什么意思? Do you mean you can see the effect of changes in the browser?你的意思是你可以在浏览器中看到更改的效果? If yes, I have the same issue wirh Firefox.如果是,我在 Firefox 上也有同样的问题。 It has nothing to do with the development server (runserver) but Firefox does not reload css files in a reliable way.它与开发服务器 (runserver) 无关,但 Firefox 不会以可靠的方式重新加载 css 文件。 Even when restarting.即使重新启动。 With other browsers I never had that issue.使用其他浏览器我从来没有遇到过这个问题。 You can try to empty cache with addons.您可以尝试使用插件清空缓存。 I read about forced reload in Firefox with Shift+reload (or Ctrl?), but it did not always work in my case.我阅读了有关使用 Shift+reload(或 Ctrl?)在 Firefox 中强制重新加载的信息,但在我的情况下它并不总是有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM