Django get Foreign key - multiple image

Hello guys i’ve a 2 model like this

class Proje_Isleri(models.Model):
    Isim = models.CharField(verbose_name="İsim", max_length=100)

    def __str__(self):
        return self.Isim

class Proje_Isleri_Resim(models.Model):
    Proje_Isim = models.ForeignKey(Proje_Isleri, default=None, related_name='projeisim', on_delete=models.CASCADE, verbose_name="Proje İsmi")
    Proje_Resim = models.ImageField(verbose_name="Proje Resim", upload_to="ProjeIsleri/")

    def __str__(self):
        return self.Proje_Isim.Isim

My 1. model is contains main names and second model contains images. 1 Name can have 10 image. And when i tried to get images to my templates i can’t make it. Here my views codes:

def projeisleri(request):
    projeler1 = Proje_Isleri.objects.all()
    projeler2 = Proje_Isleri_Resim.objects.all().select_related('Proje_Isim')
    return render(request, "ProjeIsleri.html", {"projeler2": projeler2, "projeler1": projeler1})

And in my template i want to use like this:

{% for proje1 in projeler1 %}
 <div class="tab-pane fade {% if == 1 %} show active {% endif %} " id="{{ proje1.Isim|slugify }}">
  <div class="row justify-content-center py-9">
   <div class="col-12 col-lg-10 col-xl-8">
    <div class="row">
     {% for foo in proje1.projeler2 %}
       <div class="col-6 col-md-3 col-lg">
        <div class="card mb-7">
          <a href="#">
           <img src=" {{ foo.projeisim.Proje_Resim.all }}" alt="Denka Metal | Projeler | {{ proje1.Isim }}" class="card-img-top">
      {% if forloop.counter|divisibleby:"3" %}
       <div class="w-100 d-none d-lg-block"></div>
      {% endif %}
     {% endfor %}
{% endfor %}

Before finish i want to clarify what i want. I want to get names of projects and the images what the name own(I can see the values are coming to template but i don’t know how to use them(I can see on django debug-toolbar)). I know my views and template codes not very good but i hope you understand.

What you’re not doing with this set of code is associating the specific projeler2 with the individual instances of projeler1. The select_related clause probably isn’t doing what you think it’s doing. You’ll find that you’re better off by changing it to:

def projeisleri(request):
    projeler1 = Proje_Isleri.objects.all().prefetch_related('proje_isleri_resim_set')

In your template, your first for loop, proje1 is an individual projeler1. Each of the related instances of Proje_Isleri_Resim can then be iterated over as:
{% for foo in proje1.proje_isleri_resim_set %}
(See the Following relationships “backwards” section of the docs.)

At this point, each foo is an individual instance of Proje_Isleri_Resim, so the image field would be referenced as foo.Proje_Resim.

Couple of style notes I would recommend:

  1. Use lower-case names for fields, not capitalized names
  2. In your context, name your entries as plurals when they represent something to be iterated over.
  3. Try to find a more descriptive name for your variables instead of “projeler1” and “projeler2”, especially when they refer to two different classes.
1 Like