DISQUS

Arthur Koziel’s Blog: Handling static files with Django

  • Jer · 1 year ago
    Thank you so much for this post! I have been very confused by the the MEDIA_URL and MEDIA_ROOT settings. They documentation doesn't explain how to use them at all. Thank you for laying it all out.
  • James · 1 year ago
    Thanks for the {{media_url}} trick on the template. I got to try it. will the media_url be available with doing anything?
  • Arthur · 1 year ago
    Yes the MEDIA_URL will be in your RequestContext as long as the "django.core.context_processors.media" is in your TEMPLATE_CONTEXT_PROCESSORS setting (which it is by default).
  • Alex · 1 year ago
    Thanks a lot for this article.
    Clear and useful!
  • leafar · 1 year ago
    You have a stray brace after "'/path/to/static_media')," ;)
  • Arthur · 1 year ago
    Fixed, thanks!
  • Daniel Stocks · 11 months ago
    Thanks alot for this, had a hard time figuring out on how to handle static media files with Django.
  • Joseph · 8 months ago
    Thanks, so much. I wonder why official docs don't clarify this point--it's very confusing to the uninitiated. I appreciate your making the effort to 'initiate' some of us!
  • 990adjustments · 7 months ago
    Thanks for the info. Would you know what may cause the static files not to be served. I've read the docs back and forth and followed your example along with countless others on the net and still nothing. I made sure my MEDIA_URL and ADMIN_MEDIA_PREFIX were different but still no static files.

    When I hit 127.0.0.1/resources/css/style.css
    I get a 404 flatpage not found error

    At a total loss...
  • arthurk · 7 months ago
    Sounds like you're using contrib.flatpages. The flatpages urlconf has a catch-all rule which tries to find the "/resources/css/style.css" in the database and then throws a 404 because it couldn't find it. It never gets to the static media pattern.

    To solve this, just make sure that the static-media pattern comes before the flatpages pattern in your urls.py.
  • 990adjustments · 7 months ago
    Yup, that did it! Thanks so much!! That was driving me nuts. It also helped me understand the patterns a bit more.

    Again, thank you for the great resource your providing!
  • SDC · 5 months ago
    It is helpful. Thanks!
  • DaveEveritt · 4 months ago
    Thanks for the post - useful. Still in the Django learning curve myself, and one mystery to me at this point is the best way to share a common CSS file across apps within a project. It might be stupidly obvious, but I can't manage it!
  • Roy · 3 months ago
    i found it very usefull, ty :)
  • Basil · 3 months ago
    It is helpful. Thanks!
  • Alfredo · 2 months ago
    The sub-domain tips have saved me 1000 headaches ;)
    Thank you!