Django:图像不显示

时间:2013-04-17 04:39:15

标签: django

我为创建用户个人资料创建了一个管理表单。

有一个用于将图像上传到服务器的字段

avatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True)

它工作正常,可以将图像上传到服务器。现在我想显示这个图像,因为我已经实现了一些代码。但它不起作用。

views.py文件中的代码

user_profile = request.user.get_profile()
return render_to_response('view_profile.html',{'profile':user_profile },context_instance=RequestContext(request))

html文件中的代码

<img src="{{ MEDIA_URL }}{{ profile.avatar}}" alt="No Profile picture" height="420" width="420"> 

我打印{{ MEDIA_URL }}{{ profile.avatar}}然后打印出来"/home/calcey/Documents/login/images/Nelumno_nucifera_open_flower_-_botanic_garden_adelaide2.jpg"。它打印正确的图像URL但不显示图像。

这是什么原因?

1 个答案:

答案 0 :(得分:0)

这里我忘记将(r'^images/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),导入urls.py文件。现在它工作正常

相关问题