Issue
i am trying to display image from db with views and in the inspact broswer i see it find my image but still return me 404 error. my models.py:
class HomePhoto(models.Model):
title = models.CharField(max_length=100)
img = models.ImageField(upload_to='home_page_images/')
view.py:
def index(request):
data = HomePhoto.objects.all()
return render(request, 'home.html',{'data':data,})
html:
{% for d in data %}
<img src="{{d.img}}" alt="Third slide">
{% endfor %}
url.py:
urlpatterns = [
path('',views.index,name='index')
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the html in the broswer inspact:
<img src="home_page_images/orangeshirt_1ZrO8ay.jpg" alt="Third slide"> <!--THIS IS THE RIGHT PATH AND THE RIGHT NAME OF THE IMAGE-->
Solution
{% for d in data %}
<img src="{{d.img.url}}" alt="Third slide">
{% endfor %}
you need to add url: d.img.url
Answered By - hiwa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.