Reputation: 39
the below code is table.py
class StColumn(tables.Column):
def render(self, value, record):
if record.status == 'warning':
self.attrs ={"td": {"bgcolor": "DeepSkyBlue"}}
elif record.status == 'ok':
self.attrs ={"td": {"bgcolor": "SandyBrown"}}
return u"%s" % (record.status.id)
class resultTable(BaseTable):
class Meta(BaseTable.Meta):
model = resultModel
attrs = {'class': 'table table-striped table-bordered table-hover row-color=green' , 'width': '70%'}
status= StColumn(accessor='status.id')
print(status)
fields = (
"field1",
"field2",
"field3",
"status",
"field5",
)
**how we can change the color of row when the status==warning and status ==ok **
Upvotes: 0
Views: 850
Reputation: 455
Display logic should not be handled in the view but in your templates instead. Take a look at this documentation for further information:
https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#if
Typically you would display data from your model through the use of Views and HTML templates. You would write a view function/class that gets called when a user goes to a particular URL. That view would use a queryset to pass the data from your model into the template. Going into that much detail here would be pretty wasteful as there is a TON of documentation available describing this process.
Basically you would need a view that looks kind of like this:
def your_view(request):
your_instances = YourModel.objects.all()
context = {
'your_instances': your_instances,
}
return render(request, 'your_html_template.html', context=context)
Then your template would look something like this:
<table>
{% for instance in your_instances %}
{% if instance.status == 'warning' %}
<tr style="background-color:#FF0000">
{% endif %}
{% if instance.status == 'ok' %}
<tr style="background-color:#000000">
{% endif %}
<td>{{ instance.field }}</td>
</tr>
{% endfor %}
</table>
Upvotes: 1