2
  1. I have a model

    class tournaments(models.Model):
        # ....
        total_rounds = models.IntegerField(max_length=11, blank=True, null=True)
        # ....
    
        def get_total_rounds_count(self):
            return self.total_rounds
    
  2. views.py:

    def tourney_view(request, offset):
        # .....
        if (offset):
            selected_tournament =  tournaments.objects.get(id=offset)
        return render_to_response('tournament.html', {'tournament': selected_tournament})
    
  3. In 'tournament.html' template i trying to loop over total_rounds:

    {% for q in tournament.get_total_rounds_count%}
    {% endfor %}
    

And got error: 'long' object is not iterable Why? My field is IntegerField, and i am simply trying to loop over integer values, but get "not iterable"

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Rusty
  • 1,341
  • 4
  • 14
  • 16

3 Answers3

3

You can either use this snippet: http://djangosnippets.org/snippets/1357/

Or define Tournament.get_total_rounds which returns range(get_total_rounds_count)

Otto Allmendinger
  • 27,448
  • 7
  • 68
  • 79
  • when i trying to use "range" i get error from django parser:Could not parse the remainder: '(tournament.get_total_rounds_count)' from 'range(tournament.get_total_rounds_count)' – Rusty Jul 08 '12 at 13:04
2

Yeah, Otto's way is best, just update your function to return the range.

 class tournaments(models.Model):
     # ....
     total_rounds = models.IntegerField(max_length=11, blank=True, null=True)
     # ....

     def get_total_rounds_count(self):
         return range(self.total_rounds)
monkut
  • 42,176
  • 24
  • 124
  • 155
0

That's because you can't use for construction with numbers

Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58
  • And what should i do if i want to loop over "total_rounds " ? – Rusty Jul 08 '12 at 13:02
  • @Rusty: You can't loop over numbers; you *can* create a loop that iterates a "total_rounds" number of times, but that's a different concept.. – Martijn Pieters Jul 08 '12 at 13:03
  • Is there some way to use "range(tournament.get_total_rounds_count)" or something equal in django template? – Rusty Jul 08 '12 at 13:06