-3

I've manually ordered a dictionary called lang_selection_list. I would like to loop over that list and use the key, val to set a select/options tag

Problem: My output created by loop seems to be in a random order and I dunno why?

Question: How do I loop over lang_selection_list so that the order of my options are in the same key value order i've written them in?

{% var as lang_selection_list %}
{
  "af_ALL": "Afrikaans",
  "az_ALL": "Azərbaycanca",
  "id_ALL": "Bahasa Indonesia",
  "ca_ALL": "Català",
  "si_ALL": "සිංහල",
}
{% endvar %}

    <select>
      {% for key,val in lang_selection_list.items %}
      <option value="/{{key}}/index.html">{{val}}</option>
      {% endfor %}
    </select>
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
  • 4
    Possible duplicate of [Python dictionary, keep keys/values in same order as declared](http://stackoverflow.com/questions/1867861/python-dictionary-keep-keys-values-in-same-order-as-declared) – LostMyGlasses Feb 17 '16 at 17:41
  • 2
    There is no way to order a standard ``dict``. You have to use an ``OrderedDict`` to "manually order" a dictionary. – MSeifert Feb 17 '16 at 17:42
  • If you want a list of key-value pairs, use a list of key-value pairs! Dicts are a lookup structure. They're not appropriate for every use case that involves keys and values. – user2357112 Feb 17 '16 at 17:42
  • @user2357112 how would I go about writing a variable in django keyvalue? – Armeen Moon Feb 17 '16 at 17:44
  • 3
    ``[(key1, value1), (key2, value2), ...]`` – MSeifert Feb 17 '16 at 17:47

1 Answers1

1

As mentioned in the comments there is no way to sort a regular dictionary as it has no order.

There are different approaches:

  1. OrderedDict in View

Don't create it in the template but in your view and add it to the context. If you need it in several views, create a context processor that adds it to all of your views. If you choose this way you can keep the for loop you have now. Creation of the OrderedDict would look like this:

langs = OrderedDict([
    ("af_ALL", "Afrikaans"),
    ("az_ALL", "Azərbaycanca"),
    ("id_ALL", "Bahasa Indonesia"),
    ("ca_ALL", "Català"),
    ("si_ALL", "සිංහල"),
])

See https://docs.djangoproject.com/en/1.9/ref/templates/api/#writing-your-own-context-processors on context processors.

  1. Template Solution

If you really want to create that list in your template you have to change its structure to a true list. You can either change it to a list of tuples or a list of dictionaries.

Using a list of dictionaries allows you to use the template builtin dictsort. Have a look at the documentation: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#dictsort

Note:

Personally, I would recommend not to create any data structures inside templates. Move those somewhere separate, either into a regular textfile from which to load it (you could use json or yaml to structure it), or even create a Model to make it maintainable via the Django Admin.

Risadinha
  • 16,058
  • 2
  • 88
  • 91