Django templates and variable attributes

后端 未结 6 557
忘掉有多难
忘掉有多难 2020-12-02 13:43

I\'m using Google App Engine and Django templates.
I have a table that I want to display the objects look something like:

Object Result:
    Items =          


        
6条回答
  •  孤城傲影
    2020-12-02 13:46

    Or you can use the default django system which is used to resolve attributes in tempaltes like this :

    from django.template import Variable, VariableDoesNotExist
    @register.filter
    def hash(object, attr):
        pseudo_context = { 'object' : object }
        try:
            value = Variable('object.%s' % attr).resolve(pseudo_context)
        except VariableDoesNotExist:
            value = None
    return value
    

    That just works

    in your template :

    {{ user|hash:item }}
    

提交回复
热议问题