[英]Error loading image filepath from the database in django
我開始使用django,我的一些代碼有一些小錯誤,但我不知道它在哪里。
我正在嘗試從數據庫中的文件路徑中加載圖像。
這是我用來加載圖像的models.py的一部分:
class Articulo(models.Model):
...
nombre_imagen=models.CharField(max_length=64)
如您所見,我正在從數據庫中的文件路徑加載圖像。
這是我正在嘗試執行的代碼部分:
{% if articulo %}
{% for articulo in articulo %}
....
<a href="#"><img class="card-img-top" img src="{% static '{{articulo.nombre_imagen}}'}" alt=""></a>
.....
{% endif %}
該圖像位於靜態文件中:
STATIC_URL = '/static/'
我認為這是一個愚蠢的問題,但正如我之前所說。 我是django的新人。
希望任何人都能幫助我,謝謝!
您的問題是您沒有正確調用image變量。
src="{% static '{{articulo.nombre_imagen}}'}"
應該
src="{% static articulo.nombre_imagen %}"
另外,您的圖片字段應為models.ImageField()
。
nombre_imagen=models.CharField(max_length=64)
應該
nombre_imagen=models.ImageField(upload_to="images/", blank=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.