Home > Software design >  Display product images from a loop using django
Display product images from a loop using django

Time:12-16

I would like to display my images from a loop, my images are stored on static/images and in the database the image field is a CharField of image name.

settings.py

STATICFILES_DIRS = [
   BASE_DIR / 'static',
]
STATIC_URL = 'static/'
LOGIN_REDIRECT_URL='account:profile'
LOGIN_URL='account:login'

models.py

 class Product(models.Model):
    name=models.CharField(max_length=70,blank=True,null=True)
    price=models.IntegerField()
    image=models.CharField(max_length=100,blank=True,null=True)

views.py

{% load static %}
{% for product in products %}
   <div >
     <div >
        <img src="{% static 'images/{{product.image}}' %}">     
    </div>
   </div>
{% endfor %}

I can't display the image. How to do ??

CodePudding user response:

Since you are using charfield, use this:

<img src="{% static 'images/' %}{{product.image}}">
  • Related