Skip to main content
 首页 » 编程设计

python之Django 服务器不对 CSS 进行更改

2024年08月19日26softidea

我是 Django 的新手。我启动了一个应用程序,制作了模板和静态目录,将它们添加到元素设置中并按照 this tutorial 运行服务器.然后我想更改我的 CSS 文件中的一些设置,所以我修改了一些行。但是当我再次运行 python manage.py runserver 时(我在 Windows 上运行),更改没有生效。 CSS的初始代码是:

body { 
  position: relative; 
  padding-bottom: 10px; 
  min-height: 100%; 
  background: #f7f7f7; 
} 

我改变了这样的填充结果:

body { 
  position: relative; 
  padding-bottom: 50px; 
  min-height: 100%; 
  background: #f7f7f7; 
} 

当我再次运行 django 服务器时,新代码没有生效。我用 Chrome Dev Tools 检查了代码,发现 padding 值仍然是 10px。我一直在一次又一次地运行服务器,但没有任何变化。设置中是否缺少某些内容?

请您参考如下方法:

我经常遇到同样的问题,你应该删除 staticfiles 目录。 此外,您应该在模板中的 HTML header 中添加“无缓存” header ,至少对于开发而言。

<meta http-equiv="cache-control" content="max-age=0" /> 
<meta http-equiv="cache-control" content="no-cache" /> 
<meta http-equiv="expires" content="0" /> 
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> 
<meta http-equiv="pragma" content="no-cache" /> 

如果遇到麻烦,您还可以强制浏览器清除其缓存。

如果你想重建'staticfiles'目录,你可以运行命令:

python manage.py collectstatic 

您不需要停止/重新启动 django 服务器,它对 Web 文件(HTML/JS/CSS)没有影响。