Загрузка изображения в Django внутри цикла for
Я хотел показать загруженное изображение из админки в моем шаблоне index.html. поэтому мне нужно было поставить цикл for и у меня возникла проблема с src изображения.
setting.py :
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static"
]
models.py :
class Home(models.Model):
image = models.ImageField(upload_to = "static")
titr = models.CharField(max_length=200)
description = models.TextField()
created = models.DateField(auto_now_add = True)
updated = models.DateField(auto_now = True)
class Meta:
ordering = ['created']
def __str__(self):
return str(self.titr)
views.py :
from django.shortcuts import render
from .models import Home
# Create your views here.
def index(request):
home = Home.objects.all()
context = {"homes" : home}
return render(request, 'home/index.html', context)
index.html (не работает) :
{% extends 'main.html' %}
{% block content %}
{% load static %}
{% for home in homes %}
<br>
<h1>{{home.titr}}</h1>
<img src="{% static '{{home.image.url}}' %}" style="width:50%" alt="My image">
<p>{{home.description}}</p>
<br><hr>
{% endfor %}
{% endblock content %}
index.html (работает, но это не то, что я хочу):
{% extends 'main.html' %}
{% block content %}
{% load static %}
{% for home in homes %}
<br>
<h1>{{home.titr}}</h1>
<img src="{% static '{{home.image.url}}' %}" style="width:50%" alt="My image">
<p>{{home.description}}</p>
<br><hr>
{% endfor %}
{% endblock content %}