我正在尝试在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>
答案 0 :(得分:0)
修改:基于新信息
您已经创建了上下文字典,并将其分配给context_processes
。您不会将字典的字典传递给render()
。
将您的查看代码更改为:
return render(request, 'lwp_admin/processes.html', context_processes )
在模板中,您可以像这样遍历传递的上下文
<tr>
{% for proc in process_info %}
<td>{{ proc }}</td>
{% endfor %}
</tr>
对于参考,在如何将上下文传递到模板方面可能仍然有帮助
您是否将生成的字典添加到视图上下文中?您可以在将view字典传递到模板之前,将其附加到view方法中。
如果视图是函数,则只需在构建上下文时将其传递给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 希望有帮助
答案 1 :(得分:0)
好的,最后我设法解决了这个问题:
下面列出了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>
此输出为: [