Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django template system, calling a function inside a model

People also ask

How do you call a function in Django?

To call a view function from template with Python Django, we can add a link to the URL of the view function. to add a link to the view with name delete_product in admin_page. html.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What does {% include %} do?

Q13:-What does {% include %} does? It will include another template. It will include content from another template having the same templates defined.

What is Jinja pattern in Django?

Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox. It is a text-based template language and thus can be used to generate any markup as well as source code. Jinja.


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 %}

If the method doesn't require any arguments, you can use the @property decorator and access it normally in the template.

class ChannelStatus(models.Model):
    ...
    @property
    def function_you_want_as_property(self):
        mystring = ""
        ...

For > 1 argument, use simple tags:

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

Template:

{% for i in channel_status_list %}
  {% related_deltas i 3 4 %}
{% endfor %}

(Note the change of syntax from {{ to {%)

Can take positional parameters (e.g. related_deltas i 3 second_arg=4 debug=true).


If you find that there are too many properties running around everywhere or you have a template filter for every other method that you write, another solution was suggested on IRC thanks @FunkyBob. It's a little well, erm, funky but it is nice in certain cases.

  class MethodProxy(object):
        """For consolidating into 1 method the calling of methods with various single args
        (suitable dictionary keys)

        class MyModel(models.Model):
            ...

            def magic(self, value):
                # Free the ponies

            def thing(self):
                return MethodProxy(self.magic)

        # Usage
        >>> m = MyModel()
        ...
        >>> m.thing['value'] == m.magic('value')

        # template
        {{ m.thing.value }}

        """

        def __init__(self, method):
            self.method = method
        def __getitem__(self, key):
            return self.method(key)

Another option is to define a property. See http://adam.gomaa.us/blog/2008/aug/11/the-python-property-builtin/ .

You write your function that can do pretty much anything you want. You make it a read only property. You call the property from the template.

Et voilà !!!!