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-->
CodePudding user response:
{% for d in data %}
<img src="{{d.img.url}}" alt="Third slide">
{% endfor %}
you need to add url: d.img.url
CodePudding user response:
You are forgetting about accessing the url member:
{% for d in data %}
<img src="{{d.img.url}}" alt="Third slide">
{% endfor %}
CodePudding user response:
<!--try This -->
{% load static %}
<img src="{% static 'my_app/example.jpg' %}" alt="My image">