我正在尝试通过使用变量tables.py
使用django-tables2在record.pk
中获取表的主键。我以为record.pk
最初存在,但不存在。
class TableServeur(tables.Table):
type_server = tables.Column()
memory = tables.Column(attrs={"td": {"class": "memory"}}, verbose_name="% Mem")
cpu = tables.Column(attrs={"td": {"class": "cpu"}}, verbose_name="% Cpu")
port = tables.Column()
etat = tables.Column()
# here is the problem
T1 = '<a class="btn" href="/update/{{record.pk}}" >Icon</a>'
action = CustomTemplateColumn(T1)
class Meta:
fields = ["type_server", "cpu", "memory", "port", "etat", "action"]
template_name = "django_tables2/bootstrap4.html"
views.py
def cpu_view(request,nom_client):
output = []
liste = get_proc_output()
listServers = Serveurs.objects.all() # port, tp_serveur
listNetstat = get_netstat_output() # port,pid,etat
list_pcpu = get_proc_stat(liste) # pid,cpu,mem
# we add percentage of cpu of each item
for i in range(len(liste)):
liste[i]['cpu'] = list_pcpu[i]
# we mix and reorganise the list by their pid
for x in liste:
pid = x.get('pid')
for y in listNetstat:
if pid in y.values():
output.append({**x,**y})
# we add the type of server by their port
for i in listServers:
for j in output:
if i.port in j.values():
j['type_server'] = i.type_du_serveur
# if we have switched off server, print it with different way
temp_list_1 = [i.type_du_serveur for i in listServers]
temp_list_2 = [x['type_server'] for x in output]
serveurs_eteint = list(set(temp_list_1) - set(temp_list_2))
#
for j in serveurs_eteint:
if j == k['type_server']:
output.append({'type_server':j,'memory':'--','etat':'CLOSED'})
for k in output:
k['nom_client'] = nom_client
# here we construct the table
table = TableServeur(output)
RequestConfig(request).configure(table)
return render(request, 'server/table.html', {'table': table, 'nom': nom_client, 'output': output})
那么,使用django-tbales2创建表时,有什么方法可以直接获取主键?