简体   繁体   中英

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.

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:

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.

I hope that anyone can help me, thank you!.

Your issue is that you aren't calling the image variable correctly.

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

should be

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

Also, your image field should be models.ImageField() .

nombre_imagen=models.CharField(max_length=64)

should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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