gpt4 book ai didi

Django for loop 模板(使用 for 循环打印有限的数据)

转载 作者:行者123 更新时间:2023-12-04 06:05:50 25 4
gpt4 key购买 nike

假设我有 100电影。在 for 循环的帮助下。我们可以打印所有的电影。像下面。

在 Django 模板中

{% for movie in movies.object_list %}

{% endfor %}

但是如果我只需要打印 1 我该怎么办 25 50 75 100列表中的电影?谢谢

更新:我写了这个。还有另一种选择吗?
{% for movie in movies.object_list %}
{% if forloop.counter == 25 %}

{{ movie }}

{% endif %}

{% if forloop.counter == 50 %}

{{ movie }}

{% endif %}
{% if forloop.counter == 75 %}

{{ movie }}

{% endif %}
{% if forloop.counter == 100 %}

{{ movie }}

{% endif %}
{% endfor %}
View
def movie_sort(request):
categories = Category.objects.all()
language_name = Category.objects.get(id=request.GET.get('language'))
movie_l = Movie.objects.filter(language=request.GET.get('language'),is_active=True)
# Displaying first row
for i, v in enumerate(movie_l):
if i == 0:
first_row_f = v
if i == 24:
first_row_l = v
if i == 25:
second_row_f = v
if i == 49:
second_row_l = v
if i == 50:
third_row_f = v
if i == 74:
third_row_l = v
if i == 75:
fourth_row_f = v
if i == 99:
fourth_row_l = v
################
paginator = Paginator(movie_l, 100) # Show 25 contacts per page

page = request.GET.get('page',1)
try:
movies = paginator.page(page)
except PageNotAnInteger:
movies = paginator.page(1)
except EmptyPage:
movies = paginator.page(paginator.num_pages)
return render_to_response('movie/alphabetic_list.html',locals(),
context_instance=RequestContext(request))

UPDATE2:我在 view 中编写了以下函数.如果用户单击下一页(分页器),则唯一的问题是它会显示以前的数据。意味着这个数据不会改变。有什么建议吗?
  for i, v in enumerate(movie):
if i == 0:
first_row_f = v
if i == 24:
first_row_l = v
if i == 25:
second_row_f = v
if i == 49:
second_row_l = v
if i == 50:
third_row_f = v
if i == 74:
third_row_l = v
if i == 75:
fourth_row_f = v
if i == 99:
fourth_row_l = v

最佳答案

您必须包含 enumerate(movie.object_list)

    for i, v in enumerate(movie.object_list):
if i == 0:
first_row_f = v
if i == 24:
first_row_l = v
if i == 25:
second_row_f = v
if i == 49:
second_row_l = v
if i == 50:
third_row_f = v
if i == 74:
third_row_l = v
if i == 75:
fourth_row_f = v
if i == 99:
fourth_row_l = v

关于Django for loop 模板(使用 for 循环打印有限的数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com