在Django中,如何使用列表项的字符串值来获取对象的变量值?

时间:2021-01-09 20:45:55

This is assuming that columns is a list containing Strings, each String is representing one of the object o's variable.

假设列是一个包含字符串的列表,每个字符串表示对象o的一个变量。

<tbody>
    {% for o in objects %}
    <tr>
        {% for col in columns %}
        <td>{{ o.col }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</tbody>

Example:

例子:

class Dog(models.Model):
    name = models.CharField()
    age = models.IntegerField()
    is_dead = models.BooleanField()

columns = ('name', 'age')

I cannot explicitly enter the object's variable name and must pass it as another list because I am trying to make a 'generic' template. Also, not all variables must be shown to the users.

我不能显式地输入对象的变量名,必须将其作为另一个列表传递,因为我正在尝试创建一个“通用”模板。而且,并不是所有的变量都必须显示给用户。

1 个解决方案

#1


1  

I'm not familiar enough with django to know if there's something builtin for this, but... you could just define your own version of getattr as a template filter. For some reason (I'm assuming because it's a builtin function), I wasn't able to simply register the builtin as a new template filter. Either way, this is how I've defined my version:

我对《被解救的姜戈》还不太熟悉,不知道这里面有什么东西,但是……您可以将自己的getattr版本定义为模板过滤器。由于某些原因(我假设它是一个内建函数),我不能简单地将内建函数注册为一个新的模板过滤器。不管怎样,我就是这样定义我的版本的:

# This is defined in myapp/templatetags/dog_extras.py
from django import template

register = template.Library()

@register.filter
def my_getattr(obj, var):
    return getattr(obj, var)

To use it, you'll use it just like any other two arg template-filter:

要使用它,您将像使用其他两个arg模板过滤器一样使用它:

{{ o|my_getattr:col }}

Here's a full example (don't forget the "load" directive at the top!):

这里有一个完整的例子(不要忘记顶部的“load”指令!)

{% load dog_extras %}

<table>
    <tbody>
        {% for o in objects %}
        <tr>
            {% for col in columns %}
            <td>{{ o|my_getattr:col }}</td>
            {% endfor %}
        </tr>
        {% endfor %}
    </tbody>
</table>

If you've never made custom template-filters before, be sure to read the docs!

如果您以前从未做过自定义模板过滤器,请务必阅读文档!

#1


1  

I'm not familiar enough with django to know if there's something builtin for this, but... you could just define your own version of getattr as a template filter. For some reason (I'm assuming because it's a builtin function), I wasn't able to simply register the builtin as a new template filter. Either way, this is how I've defined my version:

我对《被解救的姜戈》还不太熟悉,不知道这里面有什么东西,但是……您可以将自己的getattr版本定义为模板过滤器。由于某些原因(我假设它是一个内建函数),我不能简单地将内建函数注册为一个新的模板过滤器。不管怎样,我就是这样定义我的版本的:

# This is defined in myapp/templatetags/dog_extras.py
from django import template

register = template.Library()

@register.filter
def my_getattr(obj, var):
    return getattr(obj, var)

To use it, you'll use it just like any other two arg template-filter:

要使用它,您将像使用其他两个arg模板过滤器一样使用它:

{{ o|my_getattr:col }}

Here's a full example (don't forget the "load" directive at the top!):

这里有一个完整的例子(不要忘记顶部的“load”指令!)

{% load dog_extras %}

<table>
    <tbody>
        {% for o in objects %}
        <tr>
            {% for col in columns %}
            <td>{{ o|my_getattr:col }}</td>
            {% endfor %}
        </tr>
        {% endfor %}
    </tbody>
</table>

If you've never made custom template-filters before, be sure to read the docs!

如果您以前从未做过自定义模板过滤器,请务必阅读文档!