Ticket #15791: do_not_call_in_templates.diff
File do_not_call_in_templates.diff, 2.2 KB (added by , 14 years ago) |
---|
-
docs/ref/templates/api.txt
195 195 * A variable can only be called if it has no required arguments. Otherwise, 196 196 the system will return an empty string. 197 197 198 * Occasionally you may want to turn off this feature, and tell the template 199 system to leave a variable un-called no matter what. To disable this feature, 200 set a ``do_not_call_in_templates`` attribute on the callable variable. The 201 template system then will act as if your variable is not callable. 202 198 203 * Obviously, there can be side effects when calling some variables, and 199 204 it'd be either foolish or a security hole to allow the template system 200 205 to access them. … … 207 212 208 213 To prevent this, set an ``alters_data`` attribute on the callable 209 214 variable. The template system won't call a variable if it has 210 ``alters_data=True`` set. The dynamically-generated 215 ``alters_data=True`` set, and will instead replace the variable with an 216 empty string, unconditionally. The dynamically-generated 211 217 :meth:`~django.db.models.Model.delete` and 212 218 :meth:`~django.db.models.Model.save` methods on Django model objects get 213 219 ``alters_data=True`` automatically. Example:: -
django/template/base.py
692 692 ): 693 693 raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bit, current)) # missing attribute 694 694 if callable(current): 695 if getattr(current, 'alters_data', False): 695 if getattr(current, 'do_not_call_in_templates', False): 696 pass 697 elif getattr(current, 'alters_data', False): 696 698 current = settings.TEMPLATE_STRING_IF_INVALID 697 699 else: 698 700 try: # method call (assuming no args required)