Django doesn’t permit you because it goes against MVC model, but sometime it’s useful to get output (in view level) depending on specific arguments. For example in my model i have this method:
def get_value(self, arg_to_pass):
if (arg_to_pass.is_type_A):
return self.value_for_A
else:
return self.value_for_B
In your template you’ll type {{ object.get_value }} but you can’t pass argument. You can solve this issue with templatetags.
Create a templatetag like this below:
def callMethod(obj, methodName):
method = getattr(obj, methodName)
if obj.__dict__.has_key("__callArg"):
ret = method(*obj.__callArg)
del obj.__callArg
return ret
return method()
def args(obj, arg):
if not obj.__dict__.has_key("__callArg"):
obj.__callArg = []
obj.__callArg += [arg]
return obj
register.filter("call", callMethod)
register.filter("args", args)
Ok, almost done. Now, in your template, call your method typying:
{{ object|args:arg_to_pass|call:"get_value" }}
Job done! If you have any question, leave a comment!
Source: sprklab.com
Vim is my favourite text editor, i use it pretty anywhere
I’ll share a useful trick for commenting multiple lines of code:
1) SHIFT-V to select multiple lines;
2) Type “:s/^/#”
Where “^” indicates the beginning of a line and “#” the comment symbol of your choice.
To decomment a block of code:
1) Shift-V
2) Type “:s/^#//”