Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Django serve media files?

I have set up a Django application that uses images. I think I have set up the media settings MEDIA_ROOT and MEDIA_URL correctly. However the images don't show up. Do you know what can be the problem?

Let consider this example:

The image files are under /home/www/media/app/photos and we are trying to request http://example.com/photos/123.jpg

Should I use these settings?

MEDIA\_ROOT = /home/www/media

MEDIA_URL = http://example.com/app

UPDATE: Forgot to mention that I'm using built-in development server.

like image 435
grigy Avatar asked Dec 31 '08 12:12

grigy


People also ask

How do Django media files work?

Serving the files: If you are developing though, you can just get the django development server to serve them for you. To do this, you tell it to route all request that come in to http://example.com/media to your MEDIA_ROOT and all requests that come in to http://example.com/static to your STATIC_ROOT.

How does Django handle static and media files?

Use a web server like Nginx to route traffic destined for your static files directly to the static root (configured via STATIC_ROOT ) Use WhiteNoise to serve up static files directly from the WSGI or ASGI web application server.

Where are Django media files stored?

By default, Django stores files locally, using the MEDIA_ROOT and MEDIA_URL settings. The examples below assume that you're using these defaults. However, Django provides ways to write custom file storage systems that allow you to completely customize where and how Django stores files.


2 Answers

You can just add those lines to your urls.py

from django.urls import re_path
from django.views.static import serve

urlpatterns = [
    ...
    re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), 
    ]
like image 85
Mhamed Bendenia Avatar answered Sep 28 '22 04:09

Mhamed Bendenia


FOR DEVELOPMENT ONLY

You can setup a static media server for use with their development server by doing this in your urls.py file. I have attached the code showing how I use it (along with the forced DEBUG conditionals.)

from django.conf import settings
from django.conf.urls.defaults import *      

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('', 
    (r'^$', 'views.index'),            

    # Accounts
    (r'^accounts/login/$', 'views.user_login'),
    (r'^accounts/logout/$', 'views.user_logout'),

    # Contrib Modules
    (r'^admin/(.*)', admin.site.root),
)

if settings.DEBUG :
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    )

I place my MEDIA_ROOT in a subdirectory of html/media and link to it as such in settings.py

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'html/media/').replace('\\','/')

After development is finished, the project gets deployed to the web server where static media files are then served by Apache using directives.

like image 32
Astra Avatar answered Sep 28 '22 04:09

Astra