K
K
Klaus Kater2016-05-04 20:33:54
Django
Klaus Kater, 2016-05-04 20:33:54

How to output unshuffled dict in django template?

Hello. I'm trying to display a list in a template, in quite the usual way, in the context I declared a list,

def menu_context_processor(request):
        main_menu_list = {
                'Новости':'news',
                'Статьи':'articles',
                'О компании':'about',
                'Каталог':'catalog',
                'Контакты':'contacts'
            }
        return {'main_menu_list': main_menu_list.items()}

Output in the template
{% for menu_key, menu_val in main_menu_list %}
     <li><a href="{% url menu_val %}">{{ menu_key|nbsp }}</a></li>
{% endfor %}

The problem is that the list template mixes up, and I need to output it exactly in the same sequence as it is declared. Tell me how to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
iegor, 2016-05-04
@kurojneko

Don't use a dictionary? A dictionary is a hash table, it doesn't store the sequence. Use a tuple, list, or ordered dictionary.

R
Roman Kitaev, 2016-05-05
@deliro

from collections import OrderedDict

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question