Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#29353 closed Bug (fixed)

_middleware_chain is not set for StaticFilesHandler

Reported by: koxu1996 Owned by: Claude Paroz
Component: contrib.staticfiles Version: 2.0
Severity: Normal Keywords: staticfiles middleware_chain
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by koxu1996)

I created functional_tests directory with empty init.py and test_all.py containing following code:

from django.test import LiveServerTestCase
from selenium import webdriver


class DemoTest(LiveServerTestCase):

    def test_can_get_empty_list_of_server_seeds(self):
        self.browser = webdriver.Firefox()
        
        print('Before page open')
        self.browser.get(self.live_server_url + '/')
        print('After page open')

        self.assertEqual(1, 1)

        self.browser.quit()

Running it with 'python manage.py test functional_tests/' result with success:

...
Before page open
After page open
.
----------------------------------------------------------------------
Ran 1 test in 7.048s

OK
...

But if I change LiveServerTestCase to StaticLiveServerTestCase then I am getting strange error:

$ python manage.py test functional_tests/
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Before page open
Traceback (most recent call last):
  File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__                                                                                        
    return super().__call__(environ, start_response)
  File "/usr/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 146, in __call__
    response = self.get_response(request)
  File "/usr/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 62, in get_response                                                                                    
    return super().get_response(request)
  File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py", line 81, in get_response                                                                                              
    response = self._middleware_chain(request)
TypeError: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__                                                                                        
    return super().__call__(environ, start_response)
  File "/usr/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 146, in __call__
    response = self.get_response(request)
  File "/usr/lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 62, in get_response                                                                                    
    return super().get_response(request)
  File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py", line 81, in get_response                                                                                              
    response = self._middleware_chain(request)
TypeError: 'NoneType' object is not callable
After page open
.
----------------------------------------------------------------------
Ran 1 test in 6.659s

OK
Destroying test database for alias 'default'...

Attachments (1)

issue.tar.gz (21.0 KB ) - added by koxu1996 7 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 by Tim Graham, 7 years ago

Can you please debug the issue and explain why Django is at fault? Or at least provide a sample project that reproduces the problem. I can't reproduce a problem given the information you provided.

by koxu1996, 7 years ago

Attachment: issue.tar.gz added

in reply to:  1 comment:2 by koxu1996, 7 years ago

Replying to Tim Graham:

Can you please debug the issue and explain why Django is at fault? Or at least provide a sample project that reproduces the problem. I can't reproduce a problem given the information you provided.

I added attachment with sample project.

comment:3 by koxu1996, 7 years ago

Component: Uncategorizedcontrib.staticfiles
Description: modified (diff)
Keywords: staticfiles middleware_chain added; static-files testing server removed
Summary: Static files path cannot be the same as directory name?_middleware_chain is not set for StaticFilesHandler

comment:4 by koxu1996, 7 years ago

l probably figured it out: just compare load_middleware() in BaseHandler and StaticFilesHandler. The former is setting _middleware_chain property, but second does not do anything:

class StaticFilesHandler(WSGIHandler):
...
    def load_middleware(self):
        # Middleware are already loaded for self.application; no need to reload
        # them for self.
        pass

I see two possible solutions:

  • set _middleware_chain in StaticFilesHandler.load_middleware()
  • check if _middleware_chain is not empty before calling it

comment:5 by Claude Paroz, 7 years ago

Owner: changed from nobody to Claude Paroz
Status: newassigned
Triage Stage: UnreviewedAccepted

That's an issue with a 404 response when settings.DEBUG is False, it shouldn't fallback to super().get_response(request) in that case.

comment:6 by Claude Paroz, 7 years ago

Has patch: set

in reply to:  6 ; comment:7 by koxu1996, 7 years ago

Replying to Claude Paroz:

PR

It is working, so I look forward to merge.

BTW do you know why there is 404 error? Should not be /static served even though STATICFILES_DIRS is not set and no files are collected?

comment:8 by Carlton Gibson, 7 years ago

Triage Stage: AcceptedReady for checkin

in reply to:  7 comment:9 by Claude Paroz, 7 years ago

Replying to koxu1996:

BTW do you know why there is 404 error? Should not be /static served even though STATICFILES_DIRS is not set and no files are collected?

No, StaticFilesHandler is only serving "uncollected" files, as its typical use case is to serve static files during developement, not in a deployed environment.

comment:10 by Claude Paroz <claude@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In a9189d27:

Fixed #29353 -- Made StaticFilesHandler return a 404 response when settings.DEBUG is False

comment:11 by Carlton Gibson <carlton.gibson@…>, 6 years ago

In 1fac974:

Refs #29353 -- Removed duplicated logic in StaticFilesHandler.get_response().

Thanks Sergey Fursov for spotting the issue.

comment:12 by Tim Graham <timograham@…>, 6 years ago

In add57c7e:

[2.1.x] Refs #29353 -- Removed duplicated logic in StaticFilesHandler.get_response().

Thanks Sergey Fursov for spotting the issue.

Backport of 1fac9740675b8dbea3952b58102a643c67e951e4 from master

Note: See TracTickets for help on using tickets.
Back to Top