简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM