Waarg
Serving Django static media from the dev server.

Make sure your MEDIA_URL and ADMIN_MEDIA_PREFIX do not have the same path.

If, like me, you use the Django development server to statically serve your media files (css, javascript, images, etc) during development, you’ll have come up against the head-bangingly annoying and unsolvable problem of it JUST NOT WORKING.

You’ve done everything right:

  • Syntactically correct static URL conf in your urls.py
  • Directory structure containing media files matching MEDIA_ROOT, with correct permissions.
  • MEDIA_URL context processor correctly set up and outputting into the template.
  • Correct paths to media files in your HTML.

But your media files just return 404? Tried starting and stopping the development server? Still doesn’t work?

Can’t find any mention in the Django documentation on static files?

Try this:

Make sure your ADMIN_MEDIA_PREFIX setting is DIFFERENT from the path portion of your MEDIA_URL.

If your MEDIA_URL is ‘http://127.0.0.1:8000/media’ and your ADMIN_MEDIA_PREFIX is ‘/media/’ it WILL NOT WORK.

This has caught me out so many times that I am now habitually setting my ADMIN_MEDIA_PREFIX to ‘/thisisbonkers/’ just to be sure.