假设我有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
写了以下函数。如果用户点击下一页(paginator),它显示以前的数据是唯一的问题。意味着这些数据不会改变。任何建议?
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
答案 0 :(得分:2)
如果您只想要每25个项目,@ Gautam K就会关闭:
{% for movie in movies.object_list|slice:"::25" %}
{{ movie }}
{% endfor %}
上述解决方案包括切片后缺少的冒号,适用于任何大小列表(Gautam的解决方案仅适用于100个项目列表)。
答案 1 :(得分:1)
执行此操作的两种基本方法
编辑 - 以下是如何使用分页器的示例。
选项#1
首先,修改views.py:
from django.core.paginator import Paginator, EmptyPage, PageNotInteger
from myapp.models import Movie
def movies(request):
movie_list = Movie.objects.all()
pg = Paginator(movie_list, 50) # Show 50 items per page
current_page = request.GET.page('page')
try:
movies = pg.page(page) # Grab the page from the URL, like /?page=2
except PageNotAnInteger:
movies = pg.page(1) # Start from page 1 if no page was passed
except EmptyPage:
movies = pg.page(pg.num_pages) # If invalid number pages; show last page
# You pass the paginator object, not the queryset
return render_to_response('mytemplate.html',{'movies':movies})
接下来,调整模板:
{% for movie in movies %}
{{ movie }}
{% endfor %}
{% if movies.has_previous %}
<a href="?page={{ movies.previous_page_number }}">go back</a>
{% endif %}
You are on page {{ movies.number }} of {{ movies.paginator.num_pages }}
{% if movies.has_next %}
<a href="?page={{ movies.next_page_number }}">go forward</a>
{% endif %}
答案 2 :(得分:0)
尝试使用django slice
切割列表例如尝试
根据@JeremyLewis
的建议编辑{% for movie in movies.object_list|slice "::25" %}
{{ movie }}
{% endfor %}
而不是if语句
我没有检查过代码,因此可能会有效。
答案 3 :(得分:0)
您必须加入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