问题描述
我正在使用django-compress,将来会有很长一段时间,用于我的css和js文件。工作很棒
I am using django-compress with far future expires for my css and js files. Works great.
我想为其他静态内容(图像,Flash等)做类似的事情。我可以设置一个很远的未来到期的内容,但是我必须在更改文件时手动重命名文件。
I'd like to do something similar for other static content (images, flash, etc). I can set a far future expires on this content, but I must manually rename the files when they change.
有没有更好的方法来处理?
Is there a better way to handle this?
推荐答案
使用 django-compress
,您将CSS和JS视为动态文件 - 这是罚款。但是,其他文件是静态的 - 而Django并不希望提供这些文件。应该由Apache或其他网络服务器直接提供服务。
With django-compress
, you're treating CSS and JS as dynamic files - which is fine. However, the other files are static - and Django doesn't really want to serve these. They should be served directly by Apache or other webserver.
对于Apache,您可以使用
With Apache, you'd put something like
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>
在 .htaccess
或全局Apache配置。
in .htaccess
or the global Apache configuration.
更新:回应您的评论 - 抱歉,我不明白您的问题的细微差别。您可以做的不多,以避免使用不同的URL向客户端指示文件(例如图像)已更改。这是由例如Apache和YAHOO!
Update: Responding to your comment - Sorry I didn't understand the nuance of your question. There isn't much you can do to avoid using a different URL to indicate to a client that a file (e.g. an image) has changed. This is recommended by e.g. Apache and YAHOO!
实施这个是不是太难了。分为两部分:
Implementing this is IMO not too hard. It's in two parts:
- 使用标签(这里是一个)来生成版本的URL到将出现在您的HTML中的媒体文件。版本化网址可以是插入版本的基本URL,例如
/media/3/header.png
服务器上的实际文件保持为/media / header.png
。 - 使用
mod_rewrite
这样的工具将传入的URL转换为规范值,即/ media的请求/3/header.png
转换为/media/header.png
。 更详细地介绍了该功能。
- Use a tag (here's a useful snippet) to generate versioned URLs to media files which will appear in your HTML. The versioned URL could be the base URL with the version inserted, e.g.
/media/3/header.png
where the actual file remains as/media/header.png
on the server. - Use a tool like
mod_rewrite
to convert incoming URLs to the canonical value, i.e. a request for/media/3/header.png
gets converted to/media/header.png
. This article describes the feature in more detail.
这篇关于使用Django在图像上设置远未来的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!