Returning get_FIELD_display from a dictionary-transformed list (Django)

Asked

Viewed 168 times

0

I wanted to turn 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 a dictionary returns to me:

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

Dai no template I wanted to make:

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

Question: Why doesn’t it work? In this case, get_item_display returns nothing.

1 answer

1


The way you’re doing, the get_item_display would be an attribute previously passed by the context, when in reality it is a method created for each field that has a choices setate.

See that you’ve turned your status_list in a dictionary, and is going through it no for, but this is just accessing the keys of 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 %}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.