How does Django handle multimedia files?

I created a Django application that uses images. I think I set the media settings MEDIA_ROOT and MEDIA_URL correctly. However, images are not displayed. Do you know what could be the problem?

Consider this example:

Image files are located 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 am using the integrated development server.

+7
file django static
source share
5 answers

Serving static content from Django is discouraged by the developer himself (if I'm not mistaken, it only works in debug mode). Instead, you should use a dedicated web server.

If you really need to do this, read the documentation as static files .

+8
source share

FOR DEVELOPMENT ONLY

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

 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 put my MEDIA_ROOT in the html / media subdirectory and refer to it as such in settings.py

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

After development is complete, the project will be deployed to a web server, where static media files will then be served by Apache using directives.

+11
source share

This is the correct way to display image files using ImageField. Imagine we have a user profile profile:

models.py:

 UserProfile: profilePic= models.ImageField( upload_to='PATH_TO_UPLOAD', blank=True, null=True) 

settings.py:

 MEDIA_ROOT = 'FULL_PATH_OF_MEDIA' MEDIA_URL = 'URL_OF_MEDIA' 

urls.py:

 urlpatterns = [ . . . ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

PATH_TO_UPLOAD is the path by which user data is loaded. This is a subdirectory of FULL_PATH_OF_MEDIA, which means that the downloaded file will have

 FULL_PATH_OF_MEDIA/PATH_TO_UPLOAD 

full path. Now this content can be obtained at this URL:

 SITE_NAME/URL_OF_MEDIA/PATH_TO_UPLOAD 

I also recommend reading this on static_files vs media_files

doc

+3
source share

I suspect you are getting a Django 404 page. Try directly accessing one of your images and see what happens.

If so, you need to configure the web server so that it does not send requests to the media hierarchy in Django, but instead serves them directly. Here is a snippet from my apache conf file. In the first section, Apache sends everything to Django. The second section is "SetHandler None", which says "process material under / media in the usual way."

See http://docs.djangoproject.com/en/dev/howto/deployment/modpython/ for all interesting details.

Partial httpd.conf file for PrinceOfPinot.com (pop AKA):

 <Location "/"> SetHandler python-program PythonAutoReload Off PythonDebug Off PythonPath "['/var/www/production/pop', '/usr/local/lib/python2.5/site-packages/django'] + sys.path" SetEnv DJANGO_SETTINGS_MODULE settings PythonHandler django.core.handlers.modpython </Location> <Location "/media"> SetHandler None AddOutputFilterByType DEFLATE text/html text/css application/x-javascript </Location> 
+1
source share

I know that the original question is related to the dev server, but for those looking for a working environment answer:

https://docs.djangoproject.com/en/1.8/howto/static-files/deployment/ provides guidance on working with django files in a production environment. From the tone of the manual, it seems to imply that it is best to have a separate web server for processing files, or use mod_wsgi with Apache

0
source share

All Articles