S. *_*rby 4 python django static-files
一些背景优先.我正在使用以下"技巧"来防止不希望的浏览器缓存静态文件(CSS,JS等):
<script src="{{ STATIC_URL }}js/utils.js?version=1302983029"></script>
Run Code Online (Sandbox Code Playgroud)
当版本字符串在后续页面加载时发生更改时,它会使浏览器从服务器重新获取静态文件.(谷歌"css缓存"有关此技巧的文章.)
我希望浏览器获取最新版本的静态文件,但我还希望在文件未更改时允许浏览器缓存.换句话说,当且仅当静态文件已更新时,我希望更改版本字符串.我也想自动生成版本字符串.
为此,我使用静态文件的上次修改时间作为版本字符串.我正在制作一个自定义模板标签来执行此操作:
<script src="{% versioned_static 'js/utils.js' %}"></script>
Run Code Online (Sandbox Code Playgroud)
以下是模板标签的工作原理:
import os.path
from django import template
from django.conf import settings
class VersionedStaticNode(template.Node):
...
def render(self, context):
# With the example above, self.path_string is "js/utils.js"
static_file_path = os.path.join(settings.STATIC_ROOT, self.path_string)
return '%s?version=%s' % (
os.path.join(settings.STATIC_URL, self.path_string),
int(os.path.getmtime(static_file_path))
)
Run Code Online (Sandbox Code Playgroud)
要获取静态文件的上次修改时间,我需要知道它在系统上的文件路径.我通过连接settings.STATIC_ROOT
和来自该静态根的文件的相对路径来获取此文件路径.这对于生产服务器来说都很好,因为所有静态文件都是在收集的STATIC_ROOT
.
但是,在开发服务器上(使用manage.py runserver命令),不会收集静态文件STATIC_ROOT
.那么在开发中运行时如何获取静态文件的文件路径?
(为了澄清我的目的:我要避免的缓存情况是浏览器使用新HTML和旧CSS/JS的不匹配.在生产中,这可能会极大地混淆用户;在开发中,这会让我和其他开发人员混淆,并且我们经常刷新页面/清除浏览器缓存.)
jpi*_*pic 13
如果使用django.contrib.staticfiles,这里有一个findstatic命令(django/contrib/staticfiles/management/commands/findstatic.py)的摘录应该有所帮助.它使用finders.find来定位文件.
from django.contrib.staticfiles import finders
result = finders.find(path, all=options['all'])
path = smart_unicode(path)
if result:
if not isinstance(result, (list, tuple)):
result = [result]
output = u'\n '.join(
(smart_unicode(os.path.realpath(path)) for path in result))
self.stdout.write(
smart_str(u"Found '%s' here:\n %s\n" % (path, output)))
Run Code Online (Sandbox Code Playgroud)