我试图通过Django管理员上传一个图像,然后在前端的页面或只是通过URL查看该图像。

注意,这些都在我的本地机器上。

我的设置如下:

MEDIA_ROOT = '/home/dan/mysite/media/'

MEDIA_URL = '/media/'

我已经将upload_to参数设置为'images',文件已经正确上传到目录:

'/home/dan/mysite/media/images/myimage.png'

然而,当我试图访问下面的URL图像:

http://127.0.0.1:8000/media/images/myimage.png

我得到一个404错误。

我是否需要为上传的媒体设置特定的URLconf模式?

任何建议都很感激。

谢谢。


当前回答

加入Micah Carrick对django 1.8的回答:

if settings.DEBUG:
  urlpatterns.append(url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}))

其他回答

UPDATE for Django >= 1.7

根据Django 2.1文档:为用户在开发过程中上传的文件提供服务

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)

你不再需要if设置。Django将处理DEBUG,确保它只在调试模式下使用。


Django <= 1.6的原始答案

试着把这个放到你的urls.py中

from django.conf import settings

# ... your normal urlpatterns here

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT}))

当DEBUG = True时(当你在本地计算机上运行时),你可以从Django中提供静态媒体,但是当你进入生产环境并且DEBUG = False时,你可以让你的web服务器配置提供静态媒体

对于Django 1.9,你需要根据文档添加以下代码:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

欲了解更多信息,请访问:https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-files-uploaded-by-a-user-during-development

如果您使用的是python 3.0+,则按以下方式配置您的项目

设置

STATIC_DIR = BASE_DIR / 'static'
MEDIA_DIR = BASE_DIR / 'media'
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'

主要的url

from django.conf import settings
from django.conf.urls.static import static

urlspatterns=[
........
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我是否需要为上传的媒体设置特定的URLconf模式?

是的。对于开发,这就像添加到你的URLconf一样简单:

if settings.DEBUG:
    urlpatterns += patterns('django.views.static',
        (r'media/(?P<path>.*)', 'serve', {'document_root': settings.MEDIA_ROOT}),
    )

然而,对于生产,你会想要使用Apache、lighttpd、nginx或你喜欢的web服务器来服务媒体。

下面是我使用Django 1.10.6为Django -publications应用程序交付pdf文件所做的更改:

在settings.py中使用与您相同的媒体目录定义:

MEDIA_ROOT = '/home/user/mysite/media/'

MEDIA_URL = '/media/'

由@thisisashwanipandey提供,在项目的主要urls.py中:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

在settings.py中,对@r-allela提供的答案进行了修改:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # ... the rest of your context_processors goes here ...
                'django.template.context_processors.media',
            ],
         },
    },
]