2
  1. 我有一个模型

    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. 视图.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. 在“tournament.html”模板中,我尝试遍历total_rounds:

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

并得到错误:'long' object is not iterable 为什么?我的字段是 IntegerField,我只是想循环整数值,但得到“不可迭代”

4

3 回答 3

3

您可以使用以下代码段:http ://djangosnippets.org/snippets/1357/

或者定义Tournament.get_total_rounds哪些返回range(get_total_rounds_count)

于 2012-07-08T13:02:22.010 回答
2

是的,Otto 的方式是最好的,只需更新您的函数以返回范围。

 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)
于 2012-07-08T13:11:52.910 回答
0

那是因为你不能用for数字构造

于 2012-07-08T13:00:53.913 回答