IsInstance在Django模板中? [英] IsInstance in Django template?

查看:102
本文介绍了IsInstance在Django模板中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Django模板中执行isinstance / issubclass?我意识到我可以写我自己的模板,但我很惊讶,这不是可能的,这使我认为我做错了事情或缺少一些明显的东西。



我希望显示两个不同的标记段,这取决于我在我的集​​合中迭代显示的项目类型。谢谢!

解决方案

如果所有这些都继承自常见的基本类型,但是您需要知道它是哪种类型,想要在基础上实现一个返回类型的方法,那么你可以调用,如果foo.get_type =='type1'(或任何)你的模板。 p>

Is there a way to do isinstance/issubclass in a Django template? I realise I can write my own templatetag, but I'm surprised this isn't possibly which makes me think I'm either doing something wrong or missing something obvious.

I wish to display two different segments of markup, depending on which type of item I'm displaying whilst iterative over my collection. Thanks!

解决方案

If all of these inherit from a common base type, but you need to know which type it is, you may want to just implement a method on the base that returns the type - then you can call if foo.get_type == 'type1' (or whatever) in your template.

这篇关于IsInstance在Django模板中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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