How to prevent HTTP 304 in Django test server
Asked Answered
B

1

9

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

Bicameral answered 28/4, 2010 at 14:20 Comment(1)
Hello @Augusto Men .If you dont want to use default port 8000 then you can give your port no with your command as- python manage.py runserver 8001Fuel
M
11

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.

Mitre answered 28/4, 2010 at 21:27 Comment(3)
hello @alex vasi ! I am having the same problem please eloborate your answer so that i can undeerstand how can i apply above code.Fuel
Read up on middlewares, they are easy and the django docs are awesome. For the "monkeypatch" part, that would require you to locate where the djagno module is installed, then edit django/view/static/serve as stated in the answer.Outsail
Please note when inserting this middleware, it needs to be the first middleware in the list to work. Thanks for the solution!Outsail

© 2022 - 2024 — McMap. All rights reserved.