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

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

问题描述

有没有办法在 Django 模板中做 isinstance/issubclass?我意识到我可以编写自己的模板标签,但我很惊讶这不可能让我觉得我做错了什么或遗漏了一些明显的东西.

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 foo.get_type == 'type1' (或其他).

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天全站免登陆