[英]Django template doesn't show the entire list in my view
My view function printing all of the list in terminal but it is show just 1 line in template(web ,html page) how to fix my code for like terminal output 我的视图功能在终端中打印所有列表,但在模板(web,html页面)中仅显示1行,如何为类似终端的输出修复我的代码
/views.py /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 /snmpWALK.html
{% block content %}
{{MyList}}
{% endblock %}
terminal printing 终端印刷
['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.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.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.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.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.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.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.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.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.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']
web printing 网页印刷
['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']
The indentation certainly makes your code very hard to read. 缩进肯定会使您的代码很难阅读。 I suspect the reason that you are seeing multiple lists is that your
print
call is within the for
loop. 我怀疑您看到多个列表的原因是您的
print
调用在for
循环内。 Here's how to fix that: 解决方法如下:
def snmpWALK(request):
all_lists = []
if request.method=='GET':
host= 'localhost'
oid = '1.3.6.1.2.1.1.9.1.2'
for t in nextCmd(
SnmpEngine(),
CommunityData('public'),
UdpTransportTarget((host, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
lookupMib=False,
lexicographicMode=False):
# I broke this up for purposes of formatting
# on SO. normally, I would just stick these in
# the for loop above.
errorIndication, errorStatus = t[0], t[1]
errorIndex, varBinds = t[2], t[3]
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)
# this is within the for loop!!!
print(MyList)
all_lists.append(MyList) return render(request, 'snmpWALK.html', {'MyList': all_lists})
In general, your code was fairly hard to read for those of us that like to help out on SO because (1) it was improperly indented (you can see the break
statements in the OP) and (2) it didn't follow PEP8. 通常,对于喜欢SO的我们这些人来说,您的代码很难阅读,因为(1)缩进不正确(您可以在OP中看到
break
语句),以及(2)没有遵循PEP8 。 YMMV if you want to follow these conventions / suggestions, it's just a lot easier to help you if you do. YMMV如果您想遵循这些约定/建议,则可以轻松地为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.