djangodjango-settingsmedia-url

MEDIA_URL path doesn't show up


I have a problem with my static and media settings, so my uploaded images doesn't show up on my site page.

In my "settings.py" I have:

MEDIA_ROOT = (
    os.path.join(BASE_DIR, '..', 'static', 'media')
)
MEDIA_URL = '/media/'

STATIC_ROOT = (
    os.path.join(BASE_DIR, '..', 'static'),
)
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '..', 'static'),
)

In my "models.py" I have:

expert_photo = models.ImageField(upload_to='profiles')

Some "urls.py":

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),

    (r'^tinymce/', include('tinymce.urls')),

    url(r'^experts/all/$', 'expert.views.experts', name='experts'),
    url(r'^experts/get/(?P<expert_id>\d+)/$', 'expert.views.expert', name='expert'),

)

And after all of that, when I go to my page, I see, that the picture have link, like this:

http://127.0.0.1:8000/static/profiles/i_vagin.jpg

But it must be:

http://127.0.0.1:8000/static/media/profiles/i_vagin.jpg

So how can I fix that?


Solution

  • Media files are not, by default, served during development. To enable media file serving, you need to add the following code in your urls.py file:

    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = patterns('',
        # ... the rest of your URLconf goes here ...
    ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    Source: Django docs

    Update

    You'll also need to access images in your templates like this: {{ expert_photo.url }}