Python 模型显示最后一个循环图像

Python 模型显示最后一个循环图像,python,html,django,Python,Html,Django,嘿,伙计们,我只是想知道为什么在我的for循环中,我的图像每次都显示在页面上,但在我的弹出模型中,它显示了for循环为每个模型给出的最后一张图像。我甚至尝试制作一个不同的html来显示完整视图的细节,以查看它显示的图片,它仍然显示for循环给出的最后一个图像,这是最后一个发布的图像 {% extends 'navbar.html'%} {% load staticfiles%} {% block styles %} <link rel="stylesheet" type="text

嘿,伙计们,我只是想知道为什么在我的for循环中,我的图像每次都显示在页面上,但在我的弹出模型中,它显示了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 %}

这是因为您有data target=myModal,下面有,所以这只是一个计数器,用于确保获取正确的计数。非常感谢你帮助我:是的,没错。您可以在那里使用任何值,可能是帖子的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 -->