How to pass template variable to tag in Django?
I try to pass a template variable to a tag as parameter, not sure how it works.
For instance
in template html
{{ question.author_id }}
{% monetize_slot question.author_id "questioner" %}
Here I can see the webpage show question.author_id as "2", but when I try to pass it via the tag monetize_slot, it treat it as a sta开发者_StackOverflowtic string "question.author_id", not a dynamic value 2. Does anyone find a solution for this here?
Thanks,
def monetize_slot(author_id, str):
return '%s: %d' % (str, author_id,)
register.simple_tag(monetize_slot)
Works for me..
Try to define the template variable with {% with %}
{% with question.author_id as author %}
{% monetize_slot author "questioner" %}
{% endwith %}
精彩评论