简体   繁体   English

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

In my "models.py" I have: 在我的“ models.py”中,我有:

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

Some "urls.py": 一些“ 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: 要启用媒体文件投放,您需要在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)

Source: Django docs 资料来源: Django文件

Update 更新资料

You'll also need to access images in your templates like this: {{ expert_photo.url }} 您还需要像这样访问模板中的图像: {{ expert_photo.url }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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