Django template conditional variable assignment

前端 未结 3 1015
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 21:31

I want to assign a variable do different values depending on if a variable exists, is this possible? My non working example might make it clearer:

{% if user         


        
3条回答
  •  庸人自扰
    2020-12-18 22:02

    Template tag:

    @register.assignment_tag
    def alias(obj):
        """
        Alias Tag
        """
        return obj
    

    Template:

    {% alias sorted_users.0.username as menu_user %}
    

提交回复
热议问题