user514310
user514310

Reputation:

Django template for loop (print limited data using for loop)

Suppose I have 100 movies. with the help of for loop. We can print all the movies. Like below.

In Django template

{% for movie in movies.object_list %}

    {% endfor %}

But What should I do If I have to print only 1 25 50 75 100 movie from list? thanks

UPDATE: I have written this. Is there another alternative?

{% 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 : Ive written following function in view. The only problem with this if user click on next page(paginator), its displaying the previous data. Mean this data does not change. any advise?

  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

Upvotes: 2

Views: 1503

Answers (4)

no_freedom
no_freedom

Reputation: 1961

You have to include 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

Upvotes: 0

Gautam
Gautam

Reputation: 7958

Try slicing the list by using django slice

For example try doing

Edit as suggested by @JeremyLewis

{% for movie in movies.object_list|slice "::25" %}
  {{ movie }}
{% endfor  %}

instead of the if statements

I haven't checked the code so it may work.

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174624

Two basic ways to do this

  1. pagination
  2. The MultipleObjectMixin automates the whole thing (and uses the same pagination feature)

EDIT - here is an example on how to use the paginator.

Option #1

First, modify your 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})

Next, adjust your template:

{% 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 %}

Upvotes: 1

Jeremy Lewis
Jeremy Lewis

Reputation: 1719

If you simply want every 25th item, @Gautam K is close:

{% for movie in movies.object_list|slice:"::25" %}
    {{ movie }}
{% endfor %}

The above solution includes the missing colon after slice, and works for any size list (Gautam's solution only works for a 100 item list).

Upvotes: 2

Related Questions