When `django.contrib.staticfiles` is used, `StaticFilesHandler` is used as
part of `runserver`, which causes `WSGIHandler.__init__` to be called
twice.
This, in turn, causes the middlewares to all be initialised twice.
This will affect the majority of development environments. It should not
affect production environments, as people should not be using `runserver`.
With the example middleware, you can see the class being initialised
twice.
{{{#!python
# middleware.py
class ExampleMiddleware(MiddlewareMixin):
def __init__(self, get_response=None):
super().__init__(get_response=get_response)
print("simple_middleware - class: ", time.time(), self)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27796>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Anthony King):
introduced in
https://github.com/django/django/commit/99bb7fcc1859615a7b8c2468e7b97d54853bfb10
--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:1>
Comment (by Carl Meyer):
I've always thought it a bit strange that `StaticFilesHandler` inherits
`WSGIHandler`; it seems prone to unintended effects like this one. IMO the
right fix here is to make `StaticFilesHandler` self-contained, even if
that requires a bit more verbose WSGI-handling code in it.
--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:2>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:3>
* owner: nobody => Chandrakant Kumar
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:4>
Comment (by Patrick Nicholls):
Are you still working on this Chandrakant Kumar? If not I may give it a
crack.
--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:5>
* has_patch: 0 => 1
Comment:
I was not sure about inheritance drop and as the fix was rather
straightforward, I'm still proposing a
[https://github.com/django/django/pull/8939 PR] which keeps `WSGIHandler`
inheritance.
--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:6>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:7>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"09b3e46635da8048ac94ddbf058e37ec9ef31400" 09b3e466]:
{{{
#!CommitTicketReference repository=""
revision="09b3e46635da8048ac94ddbf058e37ec9ef31400"
Fixed #27796 -- Prevented middleware being loaded twice with runserver
Thanks Tim Graham for the review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27796#comment:8>