如何获取下一个obj当循环django模型 [英] how to get the next obj when loop the django model

查看:106
本文介绍了如何获取下一个obj当循环django模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

{% for o in page_obj.object_list %}
                <tr style="color:#4A3C8C;background-color:{% cycle '#E7E7FF' '#F7F7F7' %};"> 
                  <td align="center"> {{o.terminal_id}}</td>
                  <td align="center"> {{o.time_stamp}}</td> 
                  <td align="center" class="lat"> {{o.latitude|floatformat:"6"}}</td>
                  <td align="center" class="lng"> {{o.longitude|floatformat:"6"}}</td> 
                  <td align="center"> {{o.speed}}</td>
                  <td align="center"> 
                    <script>
                        $("script").last().parent().parent()
                    </script>
                  </td>
                  <td align="center"> {{o.speed}}</td>
                  <td align="center"> {{o.adress_reality}}</td> 
                </tr> 
            {%endfor%}

我想在<$ c后得到下一个objcet $ c>'o'对象,

所以如何从'o' object,

so how to get the next from the 'o' object ,

如何获取python文件中的下一个对象,我可以在html中显示:

how to get the next object in the python file that i can show this in the html :

{{o.next_obj}}

{{ o.next_obj }}

谢谢

推荐答案

你尝试使用get_next_by_FOO吗?
https:// docs.djangoproject.com/en/1.2/ref/models/instances/#django.db.models.Model.get_next_by_FOO

did you try to use get_next_by_FOO ? https://docs.djangoproject.com/en/1.2/ref/models/instances/#django.db.models.Model.get_next_by_FOO

如果字段不是生成的,你可以添加到你的模型(天真):

If the field is not generated on the fly, you could add to your model (naively):

class MyObj(models.Model):
    lat = ...
    lon = ...
    def get_next_by_id(self):
        return self.objects.get(id=self.id+1)

并使用 {{o.get_next_by_id.lat}}

否则它对于模板标签,请参阅标签部分,但需要更多代码。
如果你给我一个更详细的例子,你可以尝试给你一个通用的模板,这可以在你的情况下工作。

Otherwise it is a good use for the templatetags, see the tag section but require more code. If you provide me a more detailed example of what you are doing i could try to give you a generic templatetag which might work in your case.

祝你好运。

这篇关于如何获取下一个obj当循环django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆