- Author:
- michaeljtbrooks
- Posted:
- January 5, 2018
- Language:
- Python
- Version:
- Not specified
- Score:
- 1 (after 1 ratings)
Callmethod - TemplateTag to call a method on an object with arguments from within a template
{% callmethod hotel.room_price_for_night night_date="2018-01-02" room_type=room_type_context_var %}
## equals ##
>>> hotel.room_price_for_night(night_date="2018-01-02", room_type="standard") #Assuming "standard" is the value of room_type_context_var
Django doesn't allow calling a method with arguments in the template to ensure good separation of design and code logic.
However, sometimes you will be in situations where it is more maintainable to pass an argument to a method in the template than build an iterable (with the values already resolved) in a view.
Furthermore, Django doesn't strictly follow its own ideology: the {% url "url:scheme" arg, kwarg=var %} templatetag readily accepts variables as parameters!!
This template tag allows you to call a method on an object, with the specified arguments.
Usage:
{% callmethod object_var.method_name "arg1_is_a_string" arg2_is_a_var kwarg1="a string" kwarg2=another_contect_variable %}
e.g. {% callmethod hotel.room_price_for_night date="2018-01-02" room_type="standard" %} {% callmethod hotel.get_booking_tsandcs "standard" %}
NB: If for whatever reason you've ended up with a template context variable with the same name as the method you want to call on your object, you will need to force the template tag to regard that method as a string by putting it in quotes:
{# Ensure we call hotel.room_price_for_night() even though there's a template var called {{ room_price_for_night }}! #}
{% callmethod hotel."room_price_for_night" date="2018-01-02" room_type="standard" %}
- @author: Dr Michael J T Brooks
- @version: 2018-01-05
- @copyright: Onley Group 2018 (Onley Technical Consulting Ltd) http://www.onleygroup.com
- @license: MIT (use as you wish, AS IS, no warranty on performance, no liability for losses, please retain the notice)
- @write_code_GET_PAID: Want to work from home as a Django developer? Earn £30-£50 per hour ($40-$70) depending on experience for helping Onley Group develop its clients' Django-based web apps. E-mail your CV and some sample portfolio code to: [email protected]
Copyright 2018 Onley Group
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice, credits, and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Callmethod - TemplateTag
{% callmethod hotel.room_price_for_night night_date="2018-01-02" room_type=room_type_context_var %}
-equals-
hotel.room_price_for_night(night_date="2018-01-02", room_type="standard") #Assuming "standard" is the value of room_type_context_var
Django doesn't allow calling a method with arguments in the template to ensure good separation of
design and code logic.
However, sometimes you will be in situations where it is more maintainable to pass an argument
to a method in the template than build an iterable (with the values already resolved) in a view.
Furthermore, Django doesn't strictly follow its own ideology: the {% url "url:scheme" arg, kwarg=var %}
templatetag readily accepts variables as parameters!!
This template tag allows you to call a method on an object, with the specified arguments.
Usage:
{% callmethod object_var.method_name "arg1_is_a_string" arg2_is_a_var kwarg1="a string" kwarg2=another_contect_variable %}
e.g.
{% callmethod hotel.room_price_for_night date="2018-01-02" room_type="standard" %}
{% callmethod hotel.get_booking_tsandcs "standard" %}
NB: If for whatever reason you've ended up with a template context variable with the same name as the method you want to
call on your object, you will need to force the template tag to regard that method as a string by putting it in quotes:
{# Ensure we call hotel.room_price_for_night() even though there's a template var called {{ room_price_for_night }}! #}
{% callmethod hotel."room_price_for_night" date="2018-01-02" room_type="standard" %}
@author: Dr Michael J T Brooks
@version: 2018-01-05
@copyright: Onley Group 2018 (Onley Technical Consulting Ltd) http://www.onleygroup.com
@license: MIT (use as you wish, AS IS, no warranty on performance, no liability for losses, please retain the notice)
@write_code_GET_PAID: Want to work from home as a Django developer? Earn GBP30-GBP50 per hour ($40-$70) depending on experience
for helping Onley Group develop its clients' Django-based web apps.
E-mail your CV and some sample portfolio code to: freelance_developers[at]onleygroup.com
Copyright 2018 Onley Group
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice, credits, and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import unicode_literals
import re
from django.template import Library, TemplateSyntaxError, Node
from django.utils.encoding import smart_text
register = Library()
#Regex for keyword arguments
ARG_KWARG_RE = re.compile(r"(?:(\w+)=)?(.+)")
class CallMethodNode(Node):
"""
Renders the relevant value of a {% callmethod %} template tag
"""
def __init__(self, object_name, method_name, args=None, kwargs=None, asvar=False):
self.object_name_resolver = object_name
self.method_name_resolver = method_name
self.args_resolvers = args or []
self.kwargs_resolvers = kwargs or {}
self.asvar = asvar
def render(self, context):
"""
Actually renders the node based upon the context
@param context: {} The template context dict
"""
#Turn our resolvers into actual values:
try:
object_obj = self.object_name_resolver.resolve(context)
except AttributeError: #Happens if a string was passed in as the object name. Try to rescue this and treat as a var:
object_obj = context.get(self.object_name_resolver, None)
method_name = self.method_name_resolver.resolve(context) or unicode(self.method_name_resolver) #Can resolve as variable, but will also resolve as a string. Put in "inverted commas" to force string resolution
if not object_obj or not method_name:
raise TemplateSyntaxError("{{%% callmethod object_name.method_name %%}} cannot make sense of the resolved values for object_name.method_name '{object_name}.{method_name}'".format(object_name=self.object_name_resolver, method_name=self.method_name_resolver))
#Resolve the args
args = []
for arg_resolver in self.args_resolvers:
arg = arg_resolver.resolve(context)
args.append(arg)
#Resolve the kwargs
kwargs = {}
for k_raw, v_resolver in self.kwargs_resolvers.items():
k = smart_text(k_raw,'ascii')
v = v_resolver.resolve(context)
kwargs[k]=v
#Now try to call the method on the object
try:
output = getattr(object_obj, method_name)(*args, **kwargs)
except Exception as e: #Fail silently, but tell the console:
print("\033[91m{err_type} from {{%% callmethod <{obj_name}>.{method_name}() %%}}: {err_msg}\033[0m".format(err_type=e.__class__.__name__, obj_name=object_obj, method_name=method_name, err_msg=e))
output = ""
#Set to context variable if a context variable:
if self.asvar:
context[self.asvar] = output #NB: context is a dict, which is mutable :-)
return ""
return output #Otherwise return output (i.e. render this string into the page)
@register.tag
def callmethod(parser, token):
"""
Call the specified method, on the specified object, with the specified arguments and kwargs
NB: parser.compile_filter(var):
Takes the var name as a string, processes any filters that
have been used, then tries to resolve into a variable.
"""
#Check syntax:
parts = token.split_contents() #Splits apart by space (but preserves spaces in quotes)
if len(parts)<2:
raise TemplateSyntaxError("{{%% callmethod %%}} takes at least one argument (the object.its_method) e.g. {%% callmethod my_object.the_method arg1 kwarg1=a_var %%}")
#Defaults
object_name = ""
method_name = ""
args = []
kwargs = {}
#Get our object and method:
try:
obj_method_parts = parts[1].split(".")
object_name = obj_method_parts[0]
method_name = obj_method_parts[1]
except (IndexError, AttributeError):
raise TemplateSyntaxError("{{%% callmethod %%}} should have its object and method specified using dot notation: e.g. {%% callmethod my_object.the_method arg1 kwarg1=a_var %%}")
try: #Resolve any filters used to build the name
object_name = parser.compile_filter(object_name)
method_name = parser.compile_filter(method_name)
except TemplateSyntaxError as e:
print("WARNING {{%% callmethod {obj_name}.{method_name} %%}} cannot make sense of '{obj_name}'".format(obj_name=object_name, method_name=method_name))
#See if we want to dump this output into a template variable
asvar = None
if len(parts) >= 2 and parts[-2] == 'as':
asvar = parts[-1] #The context variable we are creating to store the resolved value
parts = parts[:-2] #Chew off the last two words as we've dealt with them!
#Resolve our kwargs:
args_and_kwargs = parts[2:]
if len(args_and_kwargs):
for arg_or_kwarg in args_and_kwargs:
match = ARG_KWARG_RE.match(arg_or_kwarg)
if not match:
raise TemplateSyntaxError("{{%% callmethod {obj_name}.{method_name} {arg_or_kwarg} %%}} cannot make sense of argument / kwarg '{arg_or_kwarg}'".format(obj_name=object_name, method_name=method_name, arg_or_kwarg=arg_or_kwarg))
name, value = match.groups()
resolved_value = parser.compile_filter(value)
if name:
kwargs[name] = resolved_value
else:
args.append(resolved_value)
#Hand our vars over to our rendering node:
return CallMethodNode(object_name, method_name, args, kwargs, asvar)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 8 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 3 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
try: obj_method_parts = parts[1].split(".") object_name = obj_method_parts[0] method_name = obj_method_parts[1] except (IndexError, AttributeError): raise TemplateSyntaxError("{{%% callmethod %%}} should have its object and method specified using dot notation: e.g. {%% callmethod my_object.the_method arg1 kwarg1=a_var %%}")
if we want to use constructions like obj.sub_obj.attr_1.method we need to change block to this.
#
Please login first before commenting.