简体   繁体   English

从Django中的数据库加载图像文件路径时出错

[英]Error loading image filepath from the database in django

i am starting to use django and i have a little mistake in some of my code but i dont know where is it. 我开始使用django,我的一些代码有一些小错误,但我不知道它在哪里。

I am trying to load a image from my database in a filepath. 我正在尝试从数据库中的文件路径中加载图像。

This a part from my models.py that i am using to load the image: 这是我用来加载图像的models.py的一部分:

class Articulo(models.Model):
    ...
    nombre_imagen=models.CharField(max_length=64)

As you can see, i am loading the image from a filepath in my database. 如您所见,我正在从数据库中的文件路径加载图像。

And this is the part of the code that i am trying to do that: 这是我正在尝试执行的代码部分:

{% if articulo %}
      {% for articulo in articulo %}
        ....
          <a href="#"><img class="card-img-top" img src="{% static '{{articulo.nombre_imagen}}'}" alt=""></a>
          .....
      {% endif %}

The image is located in the static files: 该图像位于静态文件中:

STATIC_URL = '/static/'

I think there is a stupid problem, but as i said before. 我认为这是一个愚蠢的问题,但正如我之前所说。 I am new at django. 我是django的新人。

I hope that anyone can help me, thank you!. 希望任何人都能帮助我,谢谢!

Your issue is that you aren't calling the image variable correctly. 您的问题是您没有正确调用image变量。

src="{% static '{{articulo.nombre_imagen}}'}"

should be 应该

src="{% static articulo.nombre_imagen %}"

Also, your image field should be models.ImageField() . 另外,您的图片字段应为models.ImageField()

nombre_imagen=models.CharField(max_length=64)

should be 应该

nombre_imagen=models.ImageField(upload_to="images/", blank=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM