Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying the Choices Field in human readable format

Tags:

python

django

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'
like image 1000
Klanestro Avatar asked Oct 22 '25 22:10

Klanestro


1 Answers

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.

like image 101
Justin O Barber Avatar answered Oct 25 '25 13:10

Justin O Barber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!