图片未显示在模板中

时间:2020-05-23 16:36:20

标签: python django

我正在尝试在页面中添加图片,但是我一直在获取

AttributeError: 'Setting' object has no attribute 'image' 

我在同一项目的其他模型中使用过Image多次,但我不知道这里可能缺少什么

这是Models.py

class Setting(models.Model):
    title = models.CharField(max_length=60)
    about_text = RichTextUploadingField(verbose_name="About Us Text")
    about_image = models.ImageField(
        blank=True, upload_to='Marketing', verbose_name="About Us Image")

    def __str__(self):
        return self.title

    def get_image_url(self):
        return "%s/%s" % (settings.MEDIA_URL, self.image)

这里是景色

def who_we_are(request):
    setting = Setting.objects.get()
    template = 'who_we_are.html'
    context = {'setting': setting}
    return render(request, template, context)

这是模板

{% extends "base.html"%}
{% block content %}
       <div class="view" style="background-image: url('{{ setting.get_image_url }}');></div>
      <div class="container pt-4">
        {{setting.about_text|safe}}
    </div>
{% endblock content %}

1 个答案:

答案 0 :(得分:0)

  • 改善这一点

  • 在模型中将self.image修改为self.about_image

def get_image_url(self):
        return "%s/%s" % (settings.MEDIA_URL, self.about_image)