Klanestro
Klanestro

Reputation: 3215

Displaying the Choices Field in human readable format

In the webpage this renders as

Status: i

How do i display the long name "Initiated" in the django template?

In my Template

<p>Status: {{ transaction.status }}</p>

models.py

class Transaction(models.Model):
    creator = models.SlugField()
    amount = models.DecimalField(max_digits= 999,decimal_places =2,null= True,default=0)
    accepted_by = models.SlugField()
    oferto_slug = models.SlugField()
    STATUSES = (
        ('i', 'Initiated'),
        ('a', 'Accepted'),
        ('d', 'Delivered'),
        ('c', 'Cancelled'),
    )
    status = models.CharField(choices=STATUSES, default='i',max_length=10)


views.py
 def __init__():
    self.state = 'i'

Upvotes: 4

Views: 2176

Answers (1)

Justin O Barber
Justin O Barber

Reputation: 11591

In your template, try this instead:

<p>Status: {{ transaction.get_status_display }}</p>

You simply add the qualified field name between get_ and _display. (There is no need for parentheses after the method name in the template.) This will give you access to the human readable form.

See the docs here.

Upvotes: 10

Related Questions