Opened 5 months ago

Last modified 5 months ago

#21451 new Bug

LiveServerTestCase returns a 404 on all URLs when MEDIA_URL = '' and DEBUG = True

Reported by: aaugustin Owned by: nobody
Component: Testing framework Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This is a fairly obscure bug and it was interesting to diagnose ;-)

I ran into it while working on the Django Debug Toolbar. Since the DDT isn't displayed when DEBUG = False, its tests run with DEBUG = True. I admit that this isn't a common use case. (Of course, I could change the function that controls whether the toolbar is shown to display it during tests regardless of DEBUG. But this isn't the point.) Since the toolbar doesn't handle any uploaded files, MEDIA_URL isn't set and defaults to the empty string.

In these circumstances, _MediaFilesHandler.should_handle(path) will always return True because any path starts with the empty string. Then _MediaFilesHandler.get_response(request) will fail to find the media file corresponding to path and catch the resulting Http404. When DEBUG = False, get_response will continue after the try/except block and call the parent get_response, which will return the expected result. But when DEBUG = True, get_response will happily return the 404 debug page! NB: all the code I've described is actually in StaticFilesHandler, which _MediaFilesHandler inherits.

I believe there are actually two bugs here:

  • _MediaFilesHandler shouldn't be applied when MEDIA_URL isn't set — and while we're there, StaticFilesHandler shouldn't be applied either when STATIC_URL isn't set.
  • get_response shouldn't swallow Http404 exceptions when DEBUG = False — this behavior has hidden the previous problem until now.

Attachments (1)

21451-patch-Django-1.6.diff (2.4 KB) - added by aaugustin 5 months ago.

Download all attachments as: .zip

Change History (3)

comment:1 Changed 5 months ago by aaugustin

I'm attaching a patch against Django 1.6 with three changes:

  • a regression test
  • a change in StaticFilesHandler.get_response that makes the test fail when DEBUG = False. I tested by commenting out the settings override.
  • a change in that makes the test pass regardless of the value of DEBUG.

I didn't spend too much time on this and I'm not sure this is the best solution.

Changed 5 months ago by aaugustin

comment:2 Changed 5 months ago by timo

  • Triage Stage changed from Unreviewed to Accepted

Add Comment

Modify Ticket

Change Properties
<Author field>
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.