如何在Django模板中获取对象的类?

时间:2021-10-15 18:06:09

If I have a list of objects that require similar layouts but need some attribute set based on the class of object how can I go about getting the class name while class and other xx values are not available within templates.

如果我有一个需要类似布局的对象列表但需要根据对象类设置一些属性,那么如何在类和其他xx值在模板中不可用时获取类名。

{% for obj in objects %}
 <div class={{obj.__class__.__name__}}
   ..
 </div>
{% endfor }}

There is probably an alternative approach i'm missing here..

可能有一种替代方法,我在这里失踪..

4 个解决方案

#1


11  

A little dirty solution

有点脏的解决方案

If objects is a QuerySet that belong to a model, you can add a custom method to your model.

如果对象是属于模型的QuerySet,则可以向模型添加自定义方法。

 class mymodel(models.Model):
     foo = models........


 def get_cname(self):
    class_name = ....
    return class_name 

then in your template you can try:

然后在您的模板中,您可以尝试:

{% for obj in objects %}
   <div class="{{obj.get_cname}}">
     ..
  </div>
{% endfor }}

#2


26  

You can also write a custom filter. My use case was to check whether or not an html element in a Django form was a checkbox. This code has been tested with Django 1.4.

您还可以编写自定义过滤器。我的用例是检查Django表单中的html元素是否为复选框。此代码已经过Django 1.4测试。

I followed the instructions about Custom Filters. My filter code looks as such.

我按照自定义过滤器的说明进行操作我的过滤器代码看起来如此。

In myapp/templatetags/class_tag.py:

在myapp / templatetags / class_tag.py中:

from django import template
register = template.Library()
@register.filter(name='get_class')
def get_class(value):
  return value.__class__.__name__

In your template file:

在您的模板文件中:

{% load class_tag %}

{% if Object|get_class == 'AClassName' %}
 do something
{% endif %}


{{ Object|get_class }}

#3


0  

a bit simpler; assuming your layout is a list of a single model:

有点简单;假设您的布局是单个模型的列表:

class ObjectListView(ListView):
    model = Person
    template_name = 'object_list.html'

    def model_name(self):
        return self.model._meta.verbose_name

Then in object_list.html:

然后在object_list.html中:

{% for obj in object_list %}
    <div class="{{view.model_name}}">...</div>
{% endfor }}

#4


0  

David's suggestion of a custom filter is great if you need this for several classes.

如果您需要多个类,David建议使用自定义过滤器。

The Django docs neglect to mention that the dev server won't detect new templatetags automatically, however, so until I restarted it by hand I was stuck with a TemplateSyntaxError class_tag is not a registered tag library.

Django文档忽略了提到dev服务器不会自动检测新的模板标签,但是,直到我手动重新启动它,我才遇到TemplateSyntaxError class_tag不是注册标记库。

.

#1


11  

A little dirty solution

有点脏的解决方案

If objects is a QuerySet that belong to a model, you can add a custom method to your model.

如果对象是属于模型的QuerySet,则可以向模型添加自定义方法。

 class mymodel(models.Model):
     foo = models........


 def get_cname(self):
    class_name = ....
    return class_name 

then in your template you can try:

然后在您的模板中,您可以尝试:

{% for obj in objects %}
   <div class="{{obj.get_cname}}">
     ..
  </div>
{% endfor }}

#2


26  

You can also write a custom filter. My use case was to check whether or not an html element in a Django form was a checkbox. This code has been tested with Django 1.4.

您还可以编写自定义过滤器。我的用例是检查Django表单中的html元素是否为复选框。此代码已经过Django 1.4测试。

I followed the instructions about Custom Filters. My filter code looks as such.

我按照自定义过滤器的说明进行操作我的过滤器代码看起来如此。

In myapp/templatetags/class_tag.py:

在myapp / templatetags / class_tag.py中:

from django import template
register = template.Library()
@register.filter(name='get_class')
def get_class(value):
  return value.__class__.__name__

In your template file:

在您的模板文件中:

{% load class_tag %}

{% if Object|get_class == 'AClassName' %}
 do something
{% endif %}


{{ Object|get_class }}

#3


0  

a bit simpler; assuming your layout is a list of a single model:

有点简单;假设您的布局是单个模型的列表:

class ObjectListView(ListView):
    model = Person
    template_name = 'object_list.html'

    def model_name(self):
        return self.model._meta.verbose_name

Then in object_list.html:

然后在object_list.html中:

{% for obj in object_list %}
    <div class="{{view.model_name}}">...</div>
{% endfor }}

#4


0  

David's suggestion of a custom filter is great if you need this for several classes.

如果您需要多个类,David建议使用自定义过滤器。

The Django docs neglect to mention that the dev server won't detect new templatetags automatically, however, so until I restarted it by hand I was stuck with a TemplateSyntaxError class_tag is not a registered tag library.

Django文档忽略了提到dev服务器不会自动检测新的模板标签,但是,直到我手动重新启动它,我才遇到TemplateSyntaxError class_tag不是注册标记库。

.