Returning get_FIELD_display from a list transformed into a dictionary (Django)

0

I wanted to make the following list into a dictionary:

lists.py

status_list = (
    ('c', 'cancelado'),
    ('elab', 'em elaboração'),
    ('p', 'pendente'),
    ('co', 'concluido'),
    ('a', 'aprovado')
)

So I did the following:

Views.py

class ProposalList(ListView):
    template_name = 'core/proposal/proposal_list.html'
    model = Proposal
    paginate_by = 10

    def get_context_data(self, **kwargs):
        context = super(ProposalList, self).get_context_data(**kwargs)
        dct = {}
        for i in status_list:
            dct[i[0]] = i[1]
        context['status'] = dct
        return context

And it returns me a dictionary:

{'p': 'pendente', 'co': 'concluido', 'elab': 'em elaboração', 'a': 'aprovado', 'c': 'cancelado'}

Dai in the template I wanted to do:

<ul class="list-inline">
    {% for item in status %}
        <li name="{{ item }}">{{ get_item_display }}</li>
    {% endfor %}
</ul>

Question : Why does not it work? In case, get_item_display does not return anything.

    
asked by anonymous 27.07.2015 / 06:50

1 answer

1

In the way you're doing, get_item_display would be an attribute previously passed by context, when in fact it's a method created for each field that has% set%.

See that you've turned your choices into a dictionary, and you're browsing through it on the for, however it's just accessing the keys in your dictionary. Following your approach, to get what you want you should do something like:

<ul class="list-inline">
{% for key, item in status %}
    <li name="{{ key }}">{{ item }}</li>
{% endfor %}

    
28.07.2015 / 12:59