Determine variable type within django template

前端 未结 5 1102
执念已碎
执念已碎 2020-12-03 21:02

I have a variable that I\'m pulling into a table that sometimes is a date and sometimes is a string. If the variable is a date, I want to change the formatting:

<         


        
5条回答
  •  不思量自难忘°
    2020-12-03 21:56

    Late to the party, but I just had this problem. The solution I went for is duck-typing, so:

    {% if action.extra_column.year %}
      {{ action.extra_column|date:"M y" }}
    {% else %}
      {{ action.extra_column }}
    {% endif %}
    

    Could you argue that this is definitely not the right way to do it? Probably. Will it get the job done without writing your own template filter and having even more code to maintain? Absolutely.

提交回复
热议问题