I have this in my models:
class Task(Record):
class Status(models.IntegerChoices):
OPEN = 1, "Open"
COMPLETED = 2, "Completed"
status = models.IntegerField(choices=Status.choices, db_index=True, default=1)
Then in my template I want to show all the statuses, so I do this in views.py:
context = {
"statuses": Task.Status.choices,
}
And in my template I loop through it:
{% for label,name in statuses %}
{{ label }}: {{ name }}
{% endfor %}
This leads to:
1: Open
2: Completed
So far, so good. But now if I use a GET parameter, I can't get things to work as I want. Say I open ?id=2 and then I run:
{% for label,name in statuses %}
{{ label }}: {{ name }}
{% if label == request.GET.id %}
YES
{% else %}
Sorry, not equal to {{ request.GET.id }}
{% endif %}
{% endfor %}
Then I expect this to show YES for the first item. But it doesn't! Somehow this evaluates to:
1: Open Sorry, not equal to 1
2: Completed Sorry, not equal to 1
I do not understand why the first item does not evaluate to true.
In your view to store this value into context and pass with template . Get data from request like this
request.GET.get('id')
and make sure data type of both must be same otherwise you will always execute else condition.