Django - Custom Template Tag passing keyword args
How can I create a custom t开发者_高级运维emplate tag to use keyword args in my template?
custom_templates.py
from django import template
register = template.Library()
@register.simple_tag
def custom_tag_field(value, kwarg1=False, kwarg2=False):
t = template.loader.get_template('some_template.html')
return t.render(template.Context({'value': value, 'kwarg1':kwarg1, 'kwarg2': kwarg2}))
template.py
{% load custom_templates %}
....
I would like to use the custom template tag with keywords args like this:
{% custom_tag_field form.somefield "value" kwarg1="somearg1" kwarg2="somearg2" %)
Here you are :
A tag that parses args ang kwargs like the url tag:
{% get_with_args_and_kwargs somevar,"sometext",kwarg1=someothervar %}
http://djangosnippets.org/snippets/1113/
Nowadays this work as you'd expect, see the docs.
精彩评论