简体   繁体   中英

Can't override default kwarg value of False in template inclusion tag

I've written this template inclusion tag:

@register.inclusion_tag('blog/post_detail.html')
def post_detail(post, show_meta=True):
    return {
        'post': post,
        'show_meta': show_meta
    }

And I call it like this:

{% post_detail post show_meta=False %}

This works just fine. The template is properly rendered with show_meta having a value of False .

But, if I change the default of show_meta to be False like this:

def post_detail(post, show_meta=False):

And then if I try to call it with {% post_detail post show_meta=True %} , the template is still rendered with show_meta having a value of False . Why?

True and False are not defined in the template context by default, and by the normal template language rules, non-existent names are treated as False. Try passing 0 and 1 instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM