2017-04-21 46 views
0

嘿家伙我只是想知道如何进入我的循环我的图像显示在页面上的每一次,但在我的弹出模型它显示了for循环给出的每个模型的最后一个图像。我甚至试图制作一个不同的html,它显示了完整视图的细节以查看它显示的图片,并且它仍然显示for循环给出的最后一张图片,这是最后张贴的图片。模型显示最后一个循环图像

{% 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 %} 

回答

1

那是因为你有data-target="#myModal"和下面你有<div class="modal fade" id="myModal"...为每个图像。

所以,每个div都有相同的id。为了工作,您应该为每个图像制作独特的图像。就像这样:

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

<div class="modal fade" id="myModal-{{ forloop.counter }}" ... <!-- this will become myModal-1 myModal-2 myModal-3 etc --> 
+0

所以这只是一个计数器来确保我们抓住了正确的计数。非常感谢帮助我:) – Mohamed

+0

是的,这是正确的。只要每次迭代都是唯一的,你可以在那里使用任何值(也许是'post'的'id')。很高兴我能帮上忙。 –

+0

谢谢你让我这么清楚,下次我会记住这一点! – Mohamed

相关问题