无法在文件夹django中显示图像

时间:2020-03-30 15:32:59

标签: python django

添加注释时,我无法在网页中显示图像,通过类似的问题尝试了几种方法,但我却无能为力

note.html

<div class="container">
            <div class="row">

                {% for sticky in Notes %}
                <div class="col-md-3">
                    <div class="box">
                        <article class="media">
                            <div class="media-left">
                                <i class="glyphicon glyphicon-bookmark"></i>
                            </div>
                            <div class="media-content">
                                <div class="content">
                                    {% if Notes.img %}
                                    <img src="{{sticky.img.url}}" class="img-responsive" /><br/> //tried Notes.img.url and also Notes.img.url
                                    {% endif %}
                                    <b>{{sticky.Title}}</b><br />
                                    {{sticky.text}}
                                </div>
                            </div>
                            <div class="media-right">
                                <a href="{% url 'del_note' sticky.id %} "
                                                    <button class="glyphicon glyphicon-trash"></button>
                                                    </a>
                            </div>

                        </article>
                    </div>
                </div>
                {% endfor %}

            </div>
        </div>

settings.py

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


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

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

下面是我的文件夹结构 folder structure image

models.py

class Note(models.Model):
    Title = models.CharField(max_length=100)
    text = models.TextField(max_length = 250)
    img = models.ImageField(upload_to='pics',height_field=None, width_field=None, max_length=100,blank=True, null=True)

    created = models.DateTimeField(auto_now_add = True)

    class Meta:
        verbose_name = ("Note")
    def __str__(self):
        return self.text

在图像源(src)字段中尝试了Notes.img.url,也尝试了Notes.sticky.img.url,但没有成功

预先感谢

1 个答案:

答案 0 :(得分:3)

sticky是您的Note对象,因此您应检查sticky.img是否具有真实性True

{% if sticky.img %}
    <img src="{{sticky.img.url}}" class="img-responsive" /><br/>
{% endif %}
相关问题