How to prevent HTTP 304 in Django test server

耗尽温柔 提交于 2019-12-07 04:59:06

问题


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:


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.



来源:https://stackoverflow.com/questions/2730274/how-to-prevent-http-304-in-django-test-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!