Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

how to change the background color of the particular row in python Django

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 **

question from:https://stackoverflow.com/questions/65908294/how-to-change-the-background-color-of-the-particular-row-in-python-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...