如何在 Django 测试服务器中防止 HTTP 304
我在 Django 中有几个项目,并且时不时地在一个和另一个之间交替。它们都有一个 /media/ 路径,由 django.views.static.serve 提供服务,并且它们都有一个 /media/css/base.css 文件。
问题是,每当我运行一个项目时,对 base.css 的请求都会返回 HTTP 304(未修改),可能是因为时间戳尚未更改。但是当我运行另一个项目时,返回相同的 304,使浏览器使用前一个项目缓存的文件(因此使用错误的样式表)。
仅供记录,以下是中间件类:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.transaction.TransactionMiddleware',
)
我始终使用默认地址 http://localhost:8000。 是否还有其他解决方案(除了使用不同的端口 - 8001、8002 等)?
I have a couple of projects in Django and alternate between one and another every now and then. All of them have a /media/ path, which is served by django.views.static.serve, and they all have a /media/css/base.css file.
The problem is, whenever I run one project, the requests to base.css return an HTTP 304 (not modified), probably because the timestamp hasn't changed. But when I run the other project, the same 304 is returned, making the browser use the file cached by the previous project (and therefore, using the wrong stylesheet).
Just for the record, here are the middleware classes:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.transaction.TransactionMiddleware',
)
I always use the default address http://localhost:8000.
Is there another solution (other than using different ports - 8001, 8002, etc.)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此推出自己的中间件:
基本上,它只是从请求中删除 HTTP_IF_MODIFIED_SINCE 标头。
事后思考:或者您可以对
django.views.static.serve
进行 Monkeypatch,并将was_modified_since
函数替换为始终返回True
的函数。You can roll your own middleware for that:
Basically, it just removes HTTP_IF_MODIFIED_SINCE header from the request.
Afterthought: Or you can monkeypatch
django.views.static.serve
and replacewas_modified_since
function by the one, that always returnsTrue
.