0

I have a Django project where I want to access some images.

I have set the paths in mySite\settings.py like so

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

My urls are defined like this:

from . import views                                                

urlpatterns = [                                                    
        url(r'^$', views.index, name='index'),                     
]

if settings.DEBUG:                                                 
    # static files (images, css, javascript, etc.)                 
    urlpatterns += patterns('',                                    
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,                  
        }),                                                        
   )

So the images are located in mySite/media/subfolder. Now I want to display my images on my page, the src for my <img>-tag are like this

/media/subfolder/foobar.png

So the full path is http://localhost:8000//media/subfolder/foobar.png (checked in the web console). However, no pictures are showing up. I also tried other different absolute paths like http://localhost:8000/subfolder/foobar.png or http://localhost:8000/foobar.png, but no images are shown.

I've read Django MEDIA_URL and MEDIA_ROOT and django media not loading, but it didn't help.

What am I missing?

Community
  • 1
  • 1
Hashirun
  • 89
  • 1
  • 9
  • DEBUG = True in settings, right? – Zack Tanner Oct 12 '15 at 19:00
  • Does `MEDIA_ROOT` have the value you think it does? You call `dirname` on `__file__` twice, so if `__file__` is *…/mySite/settings.py*, then the result will be the parent of *mySite*. Appending *media* to that won't give you *mySite/media*. – Rob Kennedy Oct 12 '15 at 19:06
  • @ZackTanner Yes, `DEBUG` is set to `True` – Hashirun Oct 12 '15 at 19:31
  • @RobKennedy I just printed out the paths, and I think it shows the correct ones: `BASE_DIR: /Projects/mySite`, `MEDIA_ROOT: /Projects/mySite/media`, `MEDIA_URL: /media/` and `__file__: /Projects/mySite/mySite/settings.py`. – Hashirun Oct 12 '15 at 19:36
  • I see. You have two levels of project directories. I wouldn't expect that, but c'est la vie. – Rob Kennedy Oct 12 '15 at 19:43
  • @RobKennedy Yeah, it's kind of weird, but when you start a new project in Django, it automatically creates that structure. – Hashirun Oct 12 '15 at 19:47

1 Answers1

0

I just noticed the mistake.

The problem was that I have put the url setting in the wrong urls.py. My mySite/urls.py had the following code:

urlpatterns = [
    url(r'^app/', include('app.urls')),
]

And in my app I had the above mentioned code. Thus, the path to my media folder was wrong. Now I moved

if settings.DEBUG:                                                 
    # static files (images, css, javascript, etc.)                 
    urlpatterns += patterns('',                                    
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,                  
        }),                                                        
   )

to mySite/urls.py and the paths are correct and the images are shown.

Hashirun
  • 89
  • 1
  • 9