【问题标题】:Django template doesn't show the entire list in my viewDjango 模板没有在我的视图中显示整个列表
【发布时间】: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


【解决方案1】:

缩进确实使您的代码很难阅读。我怀疑您看到多个列表的原因是您的 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 如果您想遵循这些约定/建议,如果您这样做,帮助您会容易得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2015-02-15
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    相关资源
    最近更新 更多