mardi 3 novembre 2015

How to access to tuple values in a If Statement inside a forloop in django templates?

I'm doing a To Do App and I have a form were one of the fields is a choice field were you can choose between 3 item_importance (Important, Normal and Less Important)

In the template I want to change de color of the label depending on the item importance. The thing is that I want to access the tuple value in the templates, but I don't know how.

model.py:

item_importance = (
                  ('Important', 'Important'),
                  ('Normal', 'Normal'),
                  ('Less Important', 'Less Important')
                   )
class Item(models.Model):
     ...code...
     item_importance = models.CharField(max_length=25, choices=item_importance)

Form.py:

 class CreateItemForm(forms.ModelForm):
     description = forms.CharField(widget=forms.Textarea(attrs={'rows': 3, 'cols': 85}), max_length=200)
     deadline_date = forms.DateField(widget=forms.TextInput(attrs=   {'type': 'date'}))
 class Meta:
    model = Item
    fields = ["title", "description","item_importance", "deadline_date" ]

Template: Here the if Statement is wrong, I want to access the values of the tuples depending on the color of de label.

{% for item in queryset %}
...code...
        <div class="panel-body">
            <div class="row">
                <div class="col-md-6">
                    <textarea class="form-control" rows="4" style="background-color:white; resize: none" readonly>{{ item.description }}</textarea>
                </div>
                <div class="col-md-6">

                {% if item_importance == Important %} #How do I access the value 'Important' of the tuple here? 
                    Importancia: <span class="label label-danger"> {{ item.item_importance }}</span>
                {% elif item_importance == Normal %} ##How do I access the value 'Normal' of the tuple here? 
                    Importancia: <span class="label label-warning"> {{ item.item_importance }}</span>
                {% else %}
                    Importancia: <span class="label label-success"> {{ item.item_importance }}</span>
                {% endif %}
                    {% endfor %}
                    <p> Deadline: <strong>{{ item.deadline_date }}</strong> ({{ item.deadline_date|timeuntil }})</p>

                </div>

            </div>
        </div>
    </div>
 </div>
</div>{% endfor %}   

Aucun commentaire:

Enregistrer un commentaire