【问题标题】:Get nth iteration in Django template在 Django 模板中获取第 n 次迭代
【发布时间】:2016-09-13 15:04:06
【问题描述】:

我试图仅在 Django 模板中进行第一次或第 n 次迭代。 通常我可以遍历使用,

{% for item in pModel %}
    {{ item.post }}
{% endfor %}

我需要第一次迭代,但也想知道如何获得第 n 次迭代,

{{ pModel.0.post }}` 什么都不显示,也没有错误。

我不想遍历 pModel 中的每个对象。

我已经尝试了所有组合,即

{{ pModel[0][post] }}
{{ pModel.0.[post] }}
{{ pModel[0].post }}
{{ pModel[0][post] }}
{{ pModel.[0][post] }}
{{ pModel.[0].[post] }} etc.

pModel 来自这个视图,

def profile(request, id):
    pk = id
    name = User.objects.all().filter(id=pk)
    pModel = reversed(PostModel.objects.all().filter(author = name[0]))
    # user_instance = User.objects.all().filter(username = request.user)
    return render(request, 'profile.html', {'pModel': pModel, 'current_time': timezone.now()})

下面什么都不显示,

<strong>{{ pModel.first.post }}</strong>

在同一个模板中,我使用了正确显示的 pModel,因此我知道 pModel 正在工作。完整的模板,

{% extends 'index.html' %} {% block homepage %}
<div class="post">
  {% if pModel %}
    <h3>Profile for <strong>{{ pModel.first.post }}</strong></h3>
  <p>Last logged in: {{user.last_login|timesince:current_time}} ago on {{ user.last_login }}</p>
  <p>Joined {{user.date_joined|timesince:current_time}} ago on {{ user.date_joined }}</p>
  {% endif %}
    {% if pModel %}
    <div class="table-responsive">
      <table class='table table-striped table-hover'>
        <thead>
          <tr>
            <th>{{user.username}}'s posts</th>
            <th>Topic</th>
            <th>Topic Started By</th>
            <th>Last Active</th>
            <th class="table-cell-center">Views</th>
          </tr>
        </thead>
        <tbody>
          {% for item in pModel %}
          <tr>
            <td><a href="{% url 'thread' item.topic_id %}">{{ item.post }} uuu {{ pModel.0}}</a></td>
            <td>{{ item.topic.topic }}</td>
            <!-- item.topicid.authorid_id -->
            <td><a href="{% url 'profile' user.id %}">{{ item.topic.topicAuthor }}</a></td>
            <td class="icon-nowrap">{{ item.pub_date|timesince:current_time}}</td>
            <td class="table-cell-center">{{ item.topic.views }}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    {% endif %}
</div>
{% endblock %}

【问题讨论】:

标签: python django


【解决方案1】:

您可以使用forloop.counter0 template variable。例如,要访问n'th 元素:

{% for item in pModel %}
    {% if forloop.counter0 == n %}
        {{ item.post }}
    {% endif %}
{% endfor %}

你也可以使用first作为特例:

{{ item.first.post }}

【讨论】:

  • 是的,但这仍然会在每次迭代中循环。是否可以直接进入 pModel 列表中的特定对象?
  • @ofey 实际上是的,您可以首先使用first(请参阅我的更新答案)。但是对于任意索引,您必须循环遍历。
  • 再次,确实如此,我刚刚在自己的代码库中找到了一个示例 - matrix.0.0,(查找通过索引器访问的数组的数组索引)(也请不要通过我们遗留的代码行来判断我:P)
  • @Sayse 我想说的是,如果n 本身是一个上下文变量,你不能访问序列的n'th 元素,例如this question 中所问的。跨度>
  • @Sayse 诚然,这个问题有点含糊,但 OP 特别提到:But I need to nth or 0 iteration.
【解决方案2】:

您的pModel 变量不是查询集或列表,而是reverse iterator。您不能访问迭代器的单个元素,您只能对迭代器进行一次迭代,在此过程中耗尽它。

要支持单个元素的访问,您需要将pModel 转换为sequence,例如列表:

pModel = list(reversed(PostModel.objects.filter(author = name[0])))

然后您可以访问模板中的索引:

{{ pModel.0.post }}

【讨论】:

  • 这是一个绝妙的答案,谢谢。你不知道你通过说“......你只能迭代迭代器一次,在这个过程中耗尽它”来澄清这一点。抱歉,我没有足够的代表来投票。谢谢
猜你喜欢
  • 1970-01-01
  • 2017-10-13
  • 2011-05-20
  • 1970-01-01
  • 2012-05-05
  • 2016-11-18
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多