Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
model shows last for loop image

Hey guys I was just wondering how come in my for loop my images show up each time on the page but in my pop out model it shows the last image the for loop gave out for each model. I even tried to make a different html which shows the details of the full view to see what picture it shows and it still shows the last image the for loop gave, which is the last image that gets posted up.

{% extends 'navbar.html'%}

{% load staticfiles%}

{% block styles %}
  <link rel="stylesheet" type="text/css" href="{% static 'css/accounts/profile.css' %}" />
{% endblock %}

{% block content %}

<div class="row">
  <div class="col-lg-4 col-md-6 col-xs-12" style="background-color:red">
    <h1>Profile of {{user}}</h1>
  </div>
  <div class="col-lg-4 col-md-6 col-xs-12">
  {% for post in posts%}
    <div class="thumbnail" style="background-color: yellow">
      <img src="{{post.image.url}}" class="image" data-toggle="modal" data-target="#myModal">
      <div class="middle">
        <div class="text">{{post.title}}</div>
      </div>
    </div>

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h1 class="modal-title" id="myModalLabel"><a href="{% url 'posts:detail' post.slug%}">{{post.title}}</a></h1>
            <h6>Posted: {{ post.pub_date| timesince}}</h6> by <a href="{% url 'posts:userpost' post.author.id%}">{{post.author.username}}</a>
          </div>
          <div class="modal-body">
            <div class="thumbnail">
              <img src="{{post.image.url}}" class="image" data-toggle="modal" data-target="#myModal">
            </div>
            <p>{{post.description|linebreaks|truncatechars:120}}</p>
            <p><a href="{% url 'posts:detail' post.slug%}" class="btn btn-primary" role="button">View</a></p>
          </div>
        </div>
      </div>
    </div>

  {% endfor %}
  </div>
</div>


{% endblock %}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

That's because you have data-target="#myModal" and below you have <div class="modal fade" id="myModal"... for each image.

So, each div has the same id. In order to work, you should make that unique for each image. Like this:

data-target="#myModal-{{ forloop.counter }}">  <!--  this will become #myModal-1 #myModal-2 #myModal-3 etc -->

and

<div class="modal fade" id="myModal-{{ forloop.counter }}" ...  <!--  this will become myModal-1 myModal-2 myModal-3 etc -->
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!