媒体图片未显示,即使他们已成功加载页面

时间:2017-03-09 23:59:51

标签: python django

用户上传的图像存储在media文件夹中,与大多数Django应用程序一样。但是由于某种原因,我在我的应用程序中进行了一些更改后停止了显示。奇怪的是,当我单击图像div上的inspect元素时,它会显示img src并且它是正确的。当我将鼠标悬停在img src:<img src="/media/REC-2.jpg">上时,它只是说“无法加载图片”。请记住,我的所有静态文件都显示正常。只是媒体图像(通过用户Post上传的图像)没有显示。

在我看来,还有一个奇怪的事情可以表明问题是什么。这是我的代码:

choices.py

CATEGORY_CHOICES = (
    ('1', 'news'),
    ('2', 'sport'),
    ('3', 'technology'),
)

网址

url(r'^(?P<category>\w+)/', boxes_view, name='bv'),

视图

def boxes_view(request, category):
    print('initial', category)

    for a, b in CATEGORY_CHOICES:
        if b == category:
            category = a

    print('category', category)

    posts = Post.objects.filter(category=category)
    for post in posts:
        print('POST', post.id)

    return render(request, 'boxes.html', {'posts': posts})

这是加载页面后我的终端中出现的内容(http://127.0.0.1:8000/news/):

initial news
category 1
POST 1
POST 2
...
POST 241
POST 242
[09/Mar/2017 23:39:33] "GET /news/ HTTP/1.1" 200 19661
[09/Mar/2017 23:39:34] "GET /static/css/base.css HTTP/1.1" 304 0
initial media
initial media
category media
initial media
category media
initial media
initial media
[09/Mar/2017 23:39:34] "GET /static/images/settingsIcon.png HTTP/1.1" 304 0
category media
[09/Mar/2017 23:39:34] "GET /media/465054_3137058941780_869886704_o_o8zCskP.jpg HTTP/1.1" 200 6370
[09/Mar/2017 23:39:34] "GET /static/images/trtLogo.png HTTP/1.1" 304 0
category media
category media
[09/Mar/2017 23:39:34] "GET /media/REC-2.jpg HTTP/1.1" 200 6370
[09/Mar/2017 23:39:34] "GET /static/images/mapIcon.png HTTP/1.1" 304 0
[09/Mar/2017 23:39:34] "GET /static/js/base.js HTTP/1.1" 304 0
[09/Mar/2017 23:39:34] "GET /media/htcStory_xoFbTkY.JPG HTTP/1.1" 200 6370
[09/Mar/2017 23:39:34] "GET /media/34_comparison.jpg HTTP/1.1" 200 6370
[09/Mar/2017 23:39:34] "GET /media/REC_aeXxNPK.jpg HTTP/1.1" 200 6370

这里发生了什么?前几行是正确的,它会在经过for循环后打印initial news,然后是category 1,然后它会遍历每个Post。然后它变得奇怪了。它可以追溯到第一个打印语句,并多次打印initial mediacategory media。在这些印刷品之间加载媒体图像。知道发生了什么以及如何解决它?

编辑:完整的urls.py

urlpatterns = [
    url(r'^more/$', more, name='more'),
    url(r'^q/', search, name='search'),
    url(r'^admin/', admin.site.urls, name='admin'),
    url(r'^post/', include('post.urls')),
    url(r'^accounts/', include('allauth.urls')),

    url(r'^profile/(?P<user>\w+)/', profile, name='profile'),

    #category
    url(r'^(?P<category>\w+)/(?P<id>\d+)/', article, name='article'),
    url(r'^(?P<category>\w+)/', boxes_view, name='bv'),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_URL = '/static/'

STATIC_ROOT = 'draft1/static'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

1 个答案:

答案 0 :(得分:0)

这个问题在这里看起来不像,但在图片上传代码中。由于每个jpg具有相同的尺寸(6370)且每个png的尺寸都为0,因此您对图像的处理方式存在问题。

事实上,除了初始GET /news/之外的所有请求似乎都失败了。来自/static/的请求的回复大小均为0,而来自/media/的请求的回复大小均为6370