L
L
Lolo2021-12-05 21:23:55
Python
Lolo, 2021-12-05 21:23:55

How to get a value from a record in a template, not a key?

I already implemented it once, but I forgot how, and I didn’t find an answer on the Internet, in short, such a problem:
there is a model:

from django.db import models

COLOR_SPISOK = (
    ('dc0000', 'Красный'),
    ('f0ef00', 'Желтый'),
)

class profilModel(models.Model):
    color = models.CharField('Цвет', choices=COLOR_SPISOK, max_length=20)

    class Meta:
        verbose_name = 'тест'


view:

from django.shortcuts import render
from platform_inform.models.profil import profilModel

def indexDef(request):
    user = profilModel.objects.get(user_pk = request.session.get('user_pk'))
    return render(request, 'site_platform_inform/index/index.html', {
        # профиль
        'user': user,
    })


sample:

{% block main %}
    <span class="text_title">{{user.color}}</span>
{% endblock main %}


How to display in the template not the key of the color field, but the value, for example, not 'dc0000', but 'Red'?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-12-05
@Pingvo

{{ user.get_color_display }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question