0

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?

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52
s_spirit
  • 111
  • 10

1 Answers1

2

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 }}

xyres
  • 20,487
  • 3
  • 56
  • 85
  • Yes, I read about that before create this topic and tried it again right now... No success... Still got `` , but need `` – s_spirit Apr 16 '15 at 09:07
  • @s_spirit Are you accessing images in your templates like this: `{{ expert_photo.url }}`? – xyres Apr 16 '15 at 09:16
  • Of course, I'm not...:/ That was the answer. Previous developer take it through `{% static expert.expert_photo %}` and I missed this thing... All works right now, thank you. – s_spirit Apr 16 '15 at 09:24