最初のまたはn番目の繰り返しをDjangoテンプレートでのみ取得しようとしています。 通常、私はDjangoテンプレートのn番目の繰り返しを取得
{{pModel.0.post}} `ディスプレイ、最初の反復を必要とするだけでなく、n回目の反復を取得する方法を知りたいのですが、
{% for item in pModel %}
{{ item.post }}
{% endfor %}
を使用してを繰り返し処理することができます何もエラーがない。
私はpModelのすべてのオブジェクトを繰り返し処理したくありません。私はすべての組み合わせを試してみました
が
{{ pModel[0][post] }}
{{ pModel.0.[post] }}
{{ pModel[0].post }}
{{ pModel[0][post] }}
{{ pModel.[0][post] }}
{{ pModel.[0].[post] }} etc.
、すなわちpModelは、私が使用するのと同じテンプレートで以下の表示何も、
<strong>{{ pModel.first.post }}</strong>
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()})
、このビューから来ていません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 %}
は何ですか'pModel'? '.0.'は配列のために働くはずです。 – Sayse
[Djangoテンプレートの配列要素にアクセスするにはどうすればいいですか?](http://stackoverflow.com/questions/1700661/how-to-access-array-elements -in-a-django-template) – Sayse
pModelがビューに作成され、元の質問に追加されています。 –