MEDIA_URL路径未显示

时间:2015-04-16 08:05:22

标签: django django-settings media-url

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

在我的“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'),

)

毕竟,当我进入我的页面时,我看到图片有链接,如下所示:

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

但必须是:

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

那我该怎么办?

1 个答案:

答案 0 :(得分:2)

默认情况下,媒体文件在开发期间不会提供。要启用媒体文件服务,您需要在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 }}