How to prevent HTTP 304 in Django test server

2019-04-29 04:41发布

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

1条回答
我命由我不由天
2楼-- · 2019-04-29 05:15

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.

查看更多
登录 后发表回答