如何防止Django测试服务器中的HTTP 304 [英] How to prevent HTTP 304 in Django test server

查看:122
本文介绍了如何防止Django测试服务器中的HTTP 304的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中有几个项目,每一个和另一个之间交替。他们都有一个/ media / path,由 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等)?

解决方案

滚动自己的中间件:

  class NoIfModifiedSinceMiddleware(object):
def process_request(self,request):
request.META.pop('HTTP_IF_MODIFIED_SINCE',无)

基本上,它只是删除HTTP_IF_MODIFIED_SINCE



事后考虑:或者您可以使用monkeypatch django.views.static.serve 替换 was_modified_since 函数由一个,总是返回 True


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.)?

解决方案

You can roll your own middleware for that:

class NoIfModifiedSinceMiddleware(object):
    def process_request(self, request):
        request.META.pop('HTTP_IF_MODIFIED_SINCE', None)

Basically, it just removes HTTP_IF_MODIFIED_SINCE header from the request.

Afterthought: Or you can monkeypatch django.views.static.serve and replace was_modified_since function by the one, that always returns True.

这篇关于如何防止Django测试服务器中的HTTP 304的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆