无法显示Django Imagefield中的图像

时间:2018-08-12 05:11:17

标签: django django-2.0 imagefield

我能够使用Imagefield将图像加载到特定路径,但无法在html模板中显示这些图像。

enter image description here

我的settings.py:

STATIC_ROOT = "/app/static/"

STATIC_URL = '/static/'

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

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

文件已成功加载@ ./media /

这是我的urlpattern设置: 如果设置。调试:

urlpatterns + =静态(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)

 {% for service in services %}
    <div class="col-md-4 animate-box">
        <div class="feature-left">
            <span class="icon">
            <img src="{{ service.icon.url }}" alt=""/>
            </span>
            <div class="feature-copy">
            <h3>{{service.name}}</h3>
            <p>{{ service.text }}</p>
            <p><a href="#">Learn More <i class="icon-arrow-right22"></i></a></p>
        </div>
    </div>
   </div>
{% endfor %}

Not Found: /icon/6.png
Not Found: /icon/blog-1.jpg
Not Found: /media/6_qjtYB6t.png
Not Found: /blog/blog-3.jpg

2 个答案:

答案 0 :(得分:0)

尝试

<img src="{{ MEDIA_URL }}{{ service.icon.url }}" alt=""/>

答案 1 :(得分:0)

感谢您的关注。但是我已经解决了使用if标签的问题。现在正在工作。

{% for service in services %}
  <div class="col-md-4 animate-box">
    <div class="feature-left">
      <span class="">
        {% if service.icon %}
        <img src="{{service.icon.url}}" alt=""/>
        {% endif %}
      </span>
    <div class="feature-copy">
        <h3>{{service.name}}</h3>
        <p>{{ service.text }}</p>
        <p><a href="#">Learn More <i class="icon-arrow-right22"></i></a></p>
      </div>
    </div>
  </div>
{% endfor %}