【问题标题】:Display process list in Django template在 Django 模板中显示进程列表
【发布时间】:2019-05-28 14:58:18
【问题描述】:

我正在尝试在 Django 中实现以下示例并在表中列出所有进程,但我很难在模板中显示信息。 我在 python 文件中运行此代码,它输出多个列表(每个进程一个),如下所示

import psutil
for p in psutil.process_iter(attrs=['pid', 'username', 'status', 'cpu_percent', 'memory_percent', 'memory_info', 'name']):
    process_info = [p.pid, p.info['username'], p.info['status'], p.info['memory_info'].rss, p.info['cpu_percent'], p.info['memory_percent'], p.info['name']]
    print(process_info)

[1, 'root', 'sleeping', 9142272, 0.0, 0.009034306321108227, 'systemd']
[2, 'root', 'sleeping', 0, 0.0, 0.0, 'kthreadd']
[3, 'root', 'idle', 0, 0.0, 0.0, 'rcu_gp']
[4, 'root', 'idle', 0, 0.0, 0.0, 'rcu_par_gp']
[5, 'root', 'idle', 0, 0.0, 0.0, 'kworker/0:0-events']
[6, 'root', 'idle', 0, 0.0, 0.0, 'kworker/0:0H-kblockd']
[8, 'root', 'idle', 0, 0.0, 0.0, 'mm_percpu_wq']

如何处理所有这些单独的列表并将它们显示为表格行?

更新:这是我在views.py中的请求

def processes(request):
    for p in psutil.process_iter(attrs=['pid', 'username', 'status', 'cpu_percent', 'memory_percent', 'memory_info', 'name']):
    process_info = [p.pid, p.info['username'], p.info['status'], p.info['memory_info'].rss, p.info['cpu_percent'], p.info['memory_percent'], p.info['name']]


    context_processes = {'process_info': process_info }
    return render(request, 'lwp_admin/processes.html', context_processes )

以下是用于输出的模板中的 Django 代码:

<table class="table table-bordered table-responsive table-striped table-condensed">
                  <thead class="bg-green-gradient">
                    <tr>
                        <th scope="col" class="col-xs-1 text-center">PID</th>
                        <th scope="col" class="col-xs-1 text-center">Owner</th>
                        <th scope="col" class="col-xs-1 text-center">Status</th>
                        <th scope="col" class="col-xs-1 text-center">RSS</th>
                        <th scope="col" class="col-xs-1 text-center">CPU usage (%)</th>
                        <th scope="col" class="col-xs-1 text-center">MEM usage (%)</th>
                        <th scope="col" class="col-xs-3 text-center">Command</th>
                    </tr>
                  </thead>
                  <tbody>
                      {% for proc in context_processes.process_info %}
                      <tr>
                          <td>{{ proc }}</td>
                      </tr>
                      {% endfor %}
                  </tbody>
</table>

【问题讨论】:

  • 请将您的代码添加到您尝试在模板中添加的位置。

标签: django django-templates


【解决方案1】:

编辑:基于新信息 您已经创建了一个上下文字典并将其分配给context_processes。您不会将字典的字典传递给render()

将您的视图代码更改为:

return render(request, 'lwp_admin/processes.html', context_processes )

在您的模板中,您可以像这样循环传递的上下文

<tr>
    {% for proc in process_info %}
        <td>{{ proc }}</td>
    {% endfor %}
</tr>

对于 ref,可能对如何将上下文传递给模板仍有帮助

您是否将生成的字典添加到视图上下文中?在将其传递到模板之前,您可以在视图方法中附加到上下文字典。

如果您的视图是一个函数,您只需在构建上下文并将其传递给render(request, template, context) 函数时这样做。 https://docs.djangoproject.com/en/2.1/topics/http/views/

如果您使用基于类的视图,您可以覆盖或添加get_context_data() 方法,具体取决于您继承自的View 基类。 see example https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-display/#adding-extra-context

在您的模板中,您可以通过保存在其中的键来访问上下文值。例如

{{ name }}
{{ pid }}
{{ username }}

见模板文档https://docs.djangoproject.com/en/2.1/topics/templates/#variables 希望有帮助

【讨论】:

    【解决方案2】:

    好的,我终于设法解决了这个问题:

    views.py 代码如下所示

    def processes(request):
    proc_objects = []
    for p in psutil.process_iter(attrs=['pid', 'username', 'cpu_percent', 'memory_percent', 'memory_info', 'name']):
        process_info = [p.pid, p.info['username'], round(p.info['memory_info'].rss), p.info['cpu_percent'],p.info['memory_percent'], p.info['name']]
        proc_objects.append(process_info)
        td_buttons = ['hangup', 'terminate', 'kill']
    
    context_processes = {'proc_objects': proc_objects, 'td_buttons': td_buttons }
    return render(request, 'lwp_admin/processes.html', context_processes)
    

    模板代码如下:

     <table class="table table-bordered table-responsive table-striped table-condensed">
                  <thead class="bg-maroon-gradient">
                    <tr>
                        <th scope="col" class="col-xs-1 text-center">PID</th>
                        <th scope="col" class="col-xs-1 text-center">Owner</th>
                        <th scope="col" class="col-xs-1 text-center">RSS</th>
                        <th scope="col" class="col-xs-1 text-center">CPU usage (%)</th>
                        <th scope="col" class="col-xs-1 text-center">MEM usage (%)</th>
                        <th scope="col" class="col-xs-3 text-center">Command</th>
                        <th scope="col" class="text-center">Hang Up</th>
                        <th scope="col" class="text-center">Terminate</th>
                        <th scope="col" class="text-center">Kill</th>
                    </tr>
                  </thead>
                  <tbody>
                  {% for process_info in proc_objects %}
                      <tr class="text-center">
                          {% for proc in process_info %}
                              <td>{{ proc }}</td>
                          {% endfor %}
                          <td><button type="button" class="btn btn-primary">Hang Up</button></td>
                          <td><button type="button" class="btn btn-warning">Terminate</button></td>
                          <td><button type="button" class="btn btn-danger">Kill</button></td>
                      </tr>
                  {% endfor %}
                  </tbody>
              </table>
    

    这个的输出是: [

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2014-08-07
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多