Home > Blockchain >  How to properly pass an enum class to the .HTML template in django
How to properly pass an enum class to the .HTML template in django

Time:05-13

I have an Enum class like this one:

models.py

.
.
    class Status(Enum):
        New = 1
        Modified = 2
        Done = 3

and I want to pass this to the html template in order to iterate over it and use it.

so in my views.py I am passing it like so

views.py

from models import Status
.
.

status_options = Status    
    return render(request, 'orders.html', {status_options':status_options})

and the problem is when I try to use it inside the HTML template I don't get any values

I tried the following

orders.html

{% for status in status_options %}
    {{ status.name }}
{% endfor %}

But I don't get any output

Can anyone provide me with some guides here, please?

CodePudding user response:

In your code pass

'status_options' : [ x for x in Status ]

This is a list not a callable, so you ought to be able to iterate over it in your template:

{% for option in status_options %}
    {{ option.name }} {{option.value}}
{% endfor %}
  • Related