Django development of static media: why not MEDIA_ROOT?

I read this guide on working with static media with Django during development.

I noticed that MEDIA_URL and MEDIA_ROOT not used in this. What for? What's the difference?

I tried to do this with MEDIA_URL and MEDIA_ROOT and got weird results.

+19
python django
Feb 10 2018-10-10
source share
3 answers

In a production situation, you want your media files to be uploaded from your web server (Apache, Nginx or the like) to avoid additional workload on the Django / Python process. For this, MEDIA_URL and MEDIA_ROOT are usually used.

By launching the embedded development server, you need to set the correct URL in the url.py file. I usually use something like this:

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

which selects MEDIA_ROOT from your settings file, which means that it works for development and life.

+26
Feb 10 2018-10-10
source share

Straight from the comments in settings.py ...

MEDIA_ROOT

MEDIA_ROOT is the absolute path to the directory where the media is stored, such as /home/media/media.lawrence.com/ .

MEDIA_URL

MEDIA_URL is the URL that processes the media that is served from MEDIA_ROOT . Be sure to use a trailing slash if there is a path component (optional in other cases). Examples: " http://media.lawrence.com ", " http://example.com/media/ ".

So, to change these words ... MEDIA_ROOT is where the files are physically located on your system, and MEDIA_URL is where these files are mapped. In development, this may not always be available, and in most cases your development environment and your production environment do not match, and you need to come back and change. Another thing is that this is NOT GOOD PRACTICE when Django was designed NOT for static content for you.

If you intend to use this in development, I suggest you use a method that restricts it to DEBUG = True . Telling Django to use static content from a temporary location during development when DEBUG set to True is much better and safer. You are not going to put DEBUG into operation, right? Well, at least not worth it.

Here's how I implemented it:

settings.py:

 STATIC_DOC_ROOT = os.path.join(os.getcwd(), 'site_media') 

urls.py:

 from django.conf import settings ## debug stuff to serve static media if settings.DEBUG: urlpatterns += patterns('', (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_DOC_ROOT}), ) 

Thus, any project I'm working on has a site_media directory inside it with all the necessary media. In dev, it is autonomous, and I don’t need to flip any bits in the settings except DEBUG , which I would do anyway.

+20
Feb 10 2018-10-10
source share

Django docs recommend the following approach, which I changed for my use case:

 urlpatterns = [ # url patterns ] from django.conf import settings if settings.DEBUG: from django.conf.urls.static import static urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

Note: the above assumes that you have set MEDIA_URL and MEDIA_ROOT

... and here djangodocs linkslap .

+7
Jul 18 '16 at 21:53
source share



All Articles