Home > front end >  how can I put image from list in template
how can I put image from list in template

Time:08-03

I'm studying django and i would like to show an image of a list in the template

views.py

  data = [
            {
                "titulo": "image card 1",
                "imagem": img/image2.png",
            },
            {      
                "titulo": "image card 2",
                "imagem": "img/image1.png",
            },
        ]
return render(request, 'projects/dev.html',{'data':data})

template = index.html

<div >
    <div >
         
         {%for item in data%}
        <div >
            <div >
                <a href="#">
                    <img  src="{{item.imagem}}" alt="">
                </a>
                <div >
                    <h5 >
                        {{item.titulo}}<br>
                    </h5>
                </div>
            </div>
        </div>
        {%endfor%}
    </div>
</div>
</div>

obs: I'm using static files

CodePudding user response:

Root urls setup for static media files:

urlpatterns = []
urlpatterns = urlpatterns   static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Views:

data = Card.objects.all()
return render(request, 'projects/dev.html',{ 'data': data })

Template:

<div >
    <div >
         
         {%for item in data%}
        <div >
            <div >
                <a href="#">
                    <img  src="/{{ MEDIA_URL }}{{ item.item_image.url }}" alt="">
                </a>
                <div >
                    <h5 >
                        {{item.titulo}}<br>
                    </h5>
                </div>
            </div>
        </div>
        {%endfor%}
    </div>
</div>
</div>
  • Related