Flask Template - For Loop Iteration键:值

时间:2017-07-18 12:58:09

标签: python flask jinja2

我有一个带有Flask Jinja for循环的HTML模板,它会生成一个表格,如下所示:

<tbody>
  {% for segment in segment_details %}
    <tr>
      <td>{{segment}}</td>
      <td>{{segment_details['{{segment}}']}}</td>
    </tr>
  {% endfor %}
</tbody>

我正在尝试遍历不同长度/键的文档,并将表中的每一行显示为键和值。在我的Python代码中,我得到了这个在shell中有所需的响应:

        for item in segment_details:
            print(item, segment_details[item])

但是在Flask中,我正确地列出了所有行的项目,但是

{{segment_details['{{segment}}']}}

没有产生任何价值,我尝试过使用和不使用单引号。这可能吗?

2 个答案:

答案 0 :(得分:5)

这是一个解决方案

<tbody>
  {% for key, segment in segment_details.items() %}
    <tr>
      <td>{{ key }}</td>
      <td>{{ segment }}</td>
    </tr>
  {% endfor %}
</tbody>

答案 1 :(得分:4)

这是您的错误所在:

# import modules used in port scanner
import optparse
from socket import *
from threading import *

# connect-scan function, deals with connecting to the host / determining if 
ports are open / closed, takes arguments tgtHost, tgtPort
def connScan(tgtHost, tgtPort):
    try:
        connSkt = socket(AF_INET, SOCK_STREAM)
        connSkt.connect((tgtHost, tgtPort))
        connSkt.send('\r\n')

        result = connSkt.recv(100)
        # prints result if port is open
        print '[+] ' + str(tgtPort) + '/tcp open'

    except:
        # prints result if port is closed
        print '[-] ' + str(tgtPort) + '/tcp closed'

    finally:
        connSkt.close()

# port-scan function, takes arguments tgtHost, tgtPorts 
def portScan(tgtHost, tgtPorts):
    try:
        # tries to get target IP address
        tgtIP = gethostbyname(tgtHost)
    except:
        # if unsuccesful, prints out following result
        print '[-] cannot resolve ' + tgtHost + ': unknown host'
        return

    try:
        # tries to get target address
        tgtName = gethostbyaddr(tgtIP)
        print '\n[+] scan results for: ' + tgtName[0]
    except:
        print '\n[+] scan results for: ' + tgtIP
    # sets default time out to 1
    setdefaulttimeout(1)
    # for every port in tgtPorts
    for tgtPort in tgtPorts:
    # creates thread, target is connScan function, arguments are tgtHost, int(tgtPort)
        t = Thread(target=connScan, args=(tgtHost, int(tgtPort)))
        # starts the thread 
        t.start()

def main():
    parser = optparse.OptionParser('usage %prog -t <target-host> -p <target-port(s)>')
    parser.add_option('-t', dest='tgtHost', type='string', help='specify target host')
    parser.add_option('-p', dest='tgtPort', type='string', help='specify target port(s), seperated by a comma, seperate ranges with a -')
    (options, args) = parser.parse_args()

    if (options.tgtHost == None) | (options.tgtPort == None):
        print parser.usage
        exit(0)
    else:
        tgtHost = options.tgtHost
        # allows ranges of ports to be used, when seperated by a -
        if '-' in str(options.tgtPort):
            tgtPorts = options.tgtPort.split('-')
            tgtPorts = range(int(tgtPorts[0]),int(tgtPorts[1]))
        else:
            tgtPorts = str(options.tgtPort).split(',')

    portScan(tgtHost, tgtPorts)

if __name__ == '__main__':
    main()

内部不需要<td>{{segment_details['{{segment}}']}}</td> 。 它应该只是:

{{ }}

有关详情,请参阅Jinja的文档。 当您在<td>{{segment_details[segment]}}</td> 中撰写语句(iffor)时,使用Jinja2,但在访问变量时,请使用{% statement %}