MEDIA_URL 路径没有显示

MEDIA_URL path doesn't show up

我的静态和媒体设置有问题,所以我上传的图片没有显示在我的网站页面上。

在我的 "settings.py" 我有:

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'),
)

在我的 "models.py" 我有:

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

一些"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'),

)

所有这些之后,当我转到我的页面时,我看到图片有 link,像这样:

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

但必须是:

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

那我该如何解决呢?

默认情况下,媒体文件不会在开发过程中提供。要启用媒体文件服务,您需要在 urls.py 文件中添加以下代码:

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)

来源:Django docs

更新

您还需要像这样访问模板中的图像:{{ expert_photo.url }}