简体   繁体   English

django 中的 CSS 不更新

[英]CSS in django doesn't update

The problem that I have is not I fail to load css file, is that I can't update them.我的问题不是我无法加载 css 文件,而是我无法更新它们。 The first few times I edit after I have just created my css files, the changes were working.在我刚刚创建我的 css 文件后的前几次编辑时,更改正在起作用。 But after a while, when I try to change for example: the color, it is not changing.但是过了一会儿,当我尝试改变例如:颜色时,它并没有改变。 The colour on the web is not changing and I click to see the source file, it is using the unchanged version.网络上的颜色没有改变,我点击查看源文件,它使用的是未更改的版本。 I have also tried to use python manage.py collectstatic but still can't update the file.我也尝试使用python manage.py collectstatic但仍然无法更新文件。

# in settings.py 
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
# in main urls.py 
if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

I had this problem the other day... My issue was because the css was being cached and the cached version was being used rather than the new version.前几天我遇到了这个问题......我的问题是因为正在缓存 css 并且正在使用缓存的版本而不是新版本。

Check what the status code of the CSS being loaded is, if you're just running a dev server with "manage.py runserver" then the status code is the first number after the "GET " output.检查正在加载的 CSS 的状态代码是什么,如果您只是使用“manage.py runserver”运行开发服务器,那么状态代码是“GET”输出之后的第一个数字。

If the status code for your css is 304 then it is being cached.如果您的 css 的状态代码是 304,那么它正在被缓存。 Alternatively in Chrome and Firefox (probably other browsers too but they're what I use) open then Dev tools (Ctrl+Shift+I) and go to the "Network" tab.或者,在 Chrome 和 Firefox(也可能是其他浏览器,但我使用它们)中打开开发工具 (Ctrl+Shift+I) 并转到“网络”选项卡。 When you reload the page if the size of your css is listed as "(memory cache)" then a cached version is being used.当您重新加载页面时,如果您的 css 的大小被列为“(内存缓存)”,则正在使用缓存版本。

To reload the page loading the new css you need to reload the page without using the cache, for Chrome and Firefox you can do Ctrl+Shift+R, the status code for your CSS should then show as 200.要重新加载页面以加载新的 css,您需要在不使用缓存的情况下重新加载页面,对于 Chrome 和 Firefox,您可以执行 Ctrl+Shift+R,然后您的 CSS 的状态代码应显示为 200。

To make it easier, what I did (in Chrome), is open then Dev tools (Ctrl+Shift+I) and click the "Network" tab and check the "Disable cache", this forces the site to load all static files (including the css) from the server everytime rather than using the cached version.为了更容易,我所做的(在 Chrome 中)是打开然后开发工具(Ctrl + Shift + I)并单击“网络”选项卡并选中“禁用缓存”,这会强制站点加载所有静态文件(包括 css) 每次都来自服务器而不是使用缓存版本。

On mac, press shift then reload the page.在 Mac 上,按shift然后重新加载页面。 It's working.它正在工作。

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

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