How to render an ordered dictionary in django templates?

后端 未结 4 376
情书的邮戳
情书的邮戳 2021-01-31 14:58

I\'m trying to learning django templates but it\'s not easy.
I have a certain views.py containing a dictionary to be rendered with a template. The dictionary is made of ke

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 16:03

    Although changing the code in the view is preferable, you could also use the dictsort template tag:

    https://docs.djangoproject.com/en/stable/ref/templates/builtins/#std:templatefilter-dictsort

    
    {% for key, value in data.items|dictsort:"0.lower" %}
        
    {{ key }}: {{ value }}

提交回复
热议问题