2017-04-21 39 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

相關問題