I wanted show the uploaded image from admin url in my index.html template. so i needed to put a for loop and I got a probelm in image 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(doesn't work) :
{% 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(work but it's not what i want):
{% 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 %}