Django模板不会在我的视图中显示整个列表

问题描述 投票:0回答:1

我的视图功能打印终端中的所有列表,但它只显示模板中的1行(web,html页面)如何修复我的代码为类似的终端输出

/views.朋友

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

/SNMP walk.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']

python django list templates view
1个回答
0
投票

缩进肯定会使您的代码很难阅读。我怀疑你看到多个列表的原因是你的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)它是不正确的缩进(你可以看到OP中的break语句)和(2)它没有遵循PEP8 。 YMMV如果你想遵循这些约定/建议,如果你这样做,它会更容易帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.