Django图像无法显示

时间:2013-11-17 00:20:45

标签: django django-templates

我试图让用户能够上传个人资料图片,我的上传部分正常工作。我试图在模板中显示图片时遇到问题。这是我现在拥有的:

Urls.py:

urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.ME    DIA_ROOT,}),
#other urls
);

Settings.py:

MEDIA_ROOT = '/home/bpurdy/socialCompromise/media/'
MEDIA_URL = '/media/'

型号:

class Member(models.Model):
    STATUS = Choices('active', 'disabled')
    GENDER_CHOICES = (('Male','M'), ('Female','F'),)
    user = models.ForeignKey(User)
    created_date = models.DateTimeField(auto_now_add = True)
    updated_date = models.DateTimeField(auto_now = True)
    photo = models.ImageField("Profile Pic", upload_to="images/profiles", blank = True, null = True)
    #Other user properties

模板:(关键是用户)

{%if key.profile.photo %}
    <img src="{{ key.profile.photo.url }}" alt="{{key.profile.photo.title}}">
{%endif%}

上传后,/ home / bpurdy / socialCompromise / media / images / profiles目录中有一个文件,但是在尝试渲染页面时,我收到以下错误:

SERVER_IP_ADDRESS/media/images/profiles/Desert.jpg 404 (Not Found) And
Resource interpreted as Image but transferred with MIME type text/html: SERVER_IP_ADDRESS/search/images/profiles/Desert.jpg".

图像不会显示。

1 个答案:

答案 0 :(得分:1)

像这样尝试urls.py:

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

urlpatterns = patterns('',
    # urls
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

告诉我它是如何运作的。