为了账号安全,请及时绑定邮箱和手机立即绑定

Django 模板没有在我的视图中显示整个列表

Django 模板没有在我的视图中显示整个列表

侃侃无极 2021-11-09 16:28:34
我的视图功能在终端中打印了所有列表,但在模板(web,html 页面)中仅显示 1 行如何修复我的代码以用于类似终端输出/views.pydef 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']
查看完整描述

1 回答

?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

缩进肯定会让你的代码很难阅读。我怀疑您看到多个列表的原因是您的print呼叫在for循环内。以下是解决此问题的方法:



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})

一般来说,对于我们这些喜欢帮助解决 SO 的人来说,你的代码很难阅读,因为 (1) 它的缩进不正确(你可以看到breakOP 中的语句)和 (2) 它没有遵循 PEP8。YMMV 如果您想遵循这些约定/建议,那么这样做会更容易帮助您。


查看完整回答
反对 回复 2021-11-09
  • 1 回答
  • 0 关注
  • 146 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信