django template system, calling a function inside a model

前端 未结 5 982
遇见更好的自我
遇见更好的自我 2020-11-27 02:47

I want to call a function from my model at a template such as:

class ChannelStatus(models.Model):
 ..............................
 .........................         


        
5条回答
  •  广开言路
    2020-11-27 03:14

    You can't call a function with parameters from the template. You can only do this in the view. Alternatively you could write a custom template filter, which might look like this:

    @register.filter
    def related_deltas(obj, epk):
        return obj.get_related_deltas(epk)
    

    So now you can do this in the template:

    {% for i in channel_status_list %}
      {{ i|related_deltas:3 }}
    {% endfor %}
    

提交回复
热议问题