【发布时间】:2019-02-15 01:03:57
【问题描述】:
我的视图函数打印终端中的所有列表,但它只显示模板(web、html 页面)中的 1 行如何修复我的代码以实现类似的终端输出
/views.py
def snmpWALK(request):
if request.method=='GET':
host= 'localhost'
oid = '1.3.6.1.2.1.1.9.1.2'
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget((host, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
lookupMib=False,
lexicographicMode=False):
if errorIndication:
print(errorIndication, file=sys.stderr)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'), file=sys.stderr)
break
else:
MyList = []
for varBind in varBinds:
thing='%s = %s' % varBind
MyList.append(thing)
print (MyList)
return render(request, 'snmpWALK.html', {'MyList':MyList})
/snmpWALK.html
{% block content %}
{{MyList}}
{% endblock %}
终端打印
['1.3.6.1.2.1.1.9.1.2.1 = 1.3.6.1.6.3.11.3.1.1']
['1.3.6.1.2.1.1.9.1.2.2 = 1.3.6.1.6.3.15.2.1.1']
['1.3.6.1.2.1.1.9.1.2.3 = 1.3.6.1.6.3.10.3.1.1']
['1.3.6.1.2.1.1.9.1.2.4 = 1.3.6.1.6.3.1']
['1.3.6.1.2.1.1.9.1.2.5 = 1.3.6.1.6.3.16.2.2.1']
['1.3.6.1.2.1.1.9.1.2.6 = 1.3.6.1.2.1.49']
['1.3.6.1.2.1.1.9.1.2.7 = 1.3.6.1.2.1.4']
['1.3.6.1.2.1.1.9.1.2.8 = 1.3.6.1.2.1.50']
['1.3.6.1.2.1.1.9.1.2.9 = 1.3.6.1.6.3.13.3.1.3']
['1.3.6.1.2.1.1.9.1.2.10 = 1.3.6.1.2.1.92']
网络打印
['1.3.6.1.2.1.1.9.1.2.10 = 1.3.6.1.2.1.92']
【问题讨论】:
-
results ='%s = %s' % varBind行在for循环之外,因此它只包含最新结果。 -
不,我正在尝试 for 循环再次相同的输出
-
仅仅缩进是行不通的。您正在用最新值覆盖变量。您应该在循环之前将其初始化为一个空字符串,然后随时连接新值。最好使用列表而不是字符串。
-
我收到的所有信息都是1个字符串变量。当我定义一个列表索引时,实际上是0
-
您能否发布您的最新代码、您的预期结果和您目前看到的结果?
标签: python django list templates view