【问题标题】:image is not showing up in Django Template but found the image path图像未显示在 Django 模板中,但找到了图像路径
【发布时间】:2012-08-26 20:49:12
【问题描述】:

我为 Imagefield 使用了自定义动态路径。我的图像已完美上传到所需的文件夹中。但是图片没有出现在我的模板中。模板找到了图片路径用//,但是无法显示图片。如何解决?

这是我的设置.py

MEDIA_ROOT = '/home/faisal/workspace/pandora/media/'
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'

STATICFILES_DIRS = (
'/home/faisal/workspace/pandora/Static/' ,  
)

这是我的 urls.py

urlpatterns = patterns('',
 url(r'^admin/', include(admin.site.urls)),
 url(r'^async/gallery/',include('gallery.urls')),  
 url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': '/home/faisal/workspace/pandora/media',}),

)

这是我的模板标签

<img src="{{ MEDIA_URL }}{{ image.photo.url }}" alt="{{ image.title }}" />

这是我的views.py:

return render_to_response('gallery/image.html', 
{  
    'e_piclist' : e_piclist, 

},context_instance=RequestContext(request))   

在我的模板中,不是显示图像,而是显示图像替代。

我做错了什么?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    你的图片标签不需要{{MEDIA_ROOT}},更新标签为

    <img src="{{ image.photo.url }}" alt="{{ image.title }}" />
    

    image_field.url 提供绝对路径。参考第 3 点File field storage

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多