Home > OS >  How i can show image from database to page. Django
How i can show image from database to page. Django

Time:06-09

i have media directory in my project, and i can't take image from it. When i try getting image, then i got this
Request URL:http://127.0.0.1:8000/auto/media/img/'MyImg'.jpg and obviously it can't work, so how can i do this?
while doing this i am trying to get data in html like this

 {% for item in auto %}
    <div>
        <p>{{item.name}}</p>
        <p>{{item.info}}</p>
        <p>{{item.description}}</p>
        <img src="media/{{item.photo}}">
    </div>
<hr>
{% endfor %}

CodePudding user response:

If your Item model do have the following:

class Item(models.Model):
     ...
     photo = models.ImageField(upload_to="path within media folder", null=True, blank=True)
     ...

In your html template you can call the item.photo.url. Like this...

<img src="{{ item.photo.url }}">
  • Related