1👍
It would be far easier to move the dynamic part of the solution into your view (as mentioned in comments), and pass the correct root and leaves in the template’s context. Doing this should then make the template really straight forward.
0👍
Just in case anyone needs it, it’s actually quite simple if you look at the source for Django’s own {% load %}
tag:
Approach
- define a static tag, e.g.
{% import_dynamic_tags %}
- the parser function for this tag gives you access to the
parser
instance, which offers anadd_library()
method - the argument for
add_library
is simply a new instance ofdjango.template.base.Library
- at the new instance of
Library
, just calltag()
orfilter()
to create dynamic templatetags and filters
Example
This code must be placed inside a regular template library, e.g. myapp.templatetags.dynamic_tags
:
from django.template.base import Node, Library
register = template.Library()
class DummyNode(Node):
"""
empty DummyNode, required since a parser function
must return a Node instance
"""
def render(self, context):
return ''
@register.tag
def import_dynamic_tags(parser, token):
# create a new template library
lib = Library()
# create an "uppercase" filter dynamically. Library.filter()
# expects a name, a filter function and optional flags like takes_context
lib.filter('uppercase', lambda x: x.upper(), )
# create a custom "{% foo %}" templatetag. Library.tag()
# expects a name and a compiler function (the function you would normally
# decorate with @register_tag)
lib.tag('foo', foo_func)
# finally, tell the parser about our newly created library
parser.add_library(lib)
# filter functions MUST return a node, so just return DummyNode which
# does nothing at render time
return DummyNode()
- Django jquery not working
- Rewrite `example.com/foo/bar` to `foo.example.com/bar` with nginx. Redirect loop
- Store user specific data for each object
- Returning filtered objects(list or queryset)
Source:stackexchange.com