Home > Software engineering >  can i let the field in the html to have a specific layout when this value is not the default values
can i let the field in the html to have a specific layout when this value is not the default values

Time:12-26

i made some fields useing Django as models.CharField and set the default values to default="-"

i also made the html tamplat to show the fields. so can i let the field in the html to have a specific layout or text effect when this value is not the default values default="-"

modeks.py

Iron_Fe = models.CharField(max_length=100, default="-", blank=True, null=True)
Carbon_C = models.CharField(max_length=100, default="-", blank=True, null=True)

html

<td id="Fe" title="Iron"><sup>{{ object.Iron_Fe }}</sup>Fe</td>
<td id="C" title="Carbon"><sup>{{ object.Carbon_C }}</sup>C</td>

CodePudding user response:

You can simply use if template tags like following:

<td style="{% if object.Iron_Fe != '-' %} background-color: lightgreen; {% endif %}" 
    id="Fe" title="Iron"><sup>{{ object.Iron_Fe }}</sup>Fe
</td>

That will change background color of column to lightgreen if the value is not "-" otherwise it will do nothing.

CodePudding user response:

Got you now. You can do it this way:

<td id="C" title="Carbon" -" %}text-gray{% else %}text-green{% endif %}
"><sup>{{ object.Carbon_C }}</sup>C</td>

And a proper css:

.text-gray {
    color: gray;
}
.text-gray {
    color: green;
}
  • Related