【问题标题】:Trouble with django templating looping over a dict created from an xml responsedjango模板循环遍历从xml响应创建的dict的问题
【发布时间】:2021-01-17 13:21:20
【问题描述】:

我正在尝试通过 xml 请求在 html 页面中显示数据。我已使用 xmltodict 将 xml 内容转换为 dict。 xml数据是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<countriesRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.hotelbeds.com/schemas/messages">
    <from>1</from>
    <to>207</to>
    <total>207</total>
    <auditData processTime="30" timestamp="2020-10-01 18:10:02.666" requestHost="10.185.89.196" serverId="ip-10-185-88-253.eu-west-1.compute.internal.node.int-hbg-aws-eu-west-1.discovery" environment="[awseuwest1, awseuwest1a, ip_10_185_88_253]" release="cbd9f69ac1e7076b6ae86a8358f1e1bf26e00f44" internal="94d38b94f628408a9a0e41d7673d93a8"/>
    <countries>
        <country code="AD" isoCode="AD">
            <description>Andorra</description>
            <states>
                <state code="CA">
                    <name>CANILLO</name>
                </state>
                <state code="EN">
                    <name>ENCAMP</name>
                </state>
                <state code="ES">
                    <name>ESCALDES - ENGORDANY</name>
                </state>
                <state code="AN">
                    <name>ANDORRA</name>
                </state>
                <state code="AD">
                    <name>ANDORRA LA VELLA</name>
                </state>
                <state code="07">
                    <name>ANDORRA</name>
                </state>
                <state code="XX">
                    <name>NOT ASSIGNED</name>
                </state>
                <state code="OR">
                    <name>ORDINO</name>
                </state>
                <state code="SJ">
                    <name>SANT JULIA DE LORIA</name>
                </state>
                <state code="MS">
                    <name>LA MASSANA</name>
                </state>
            </states>
        </country>
        <country code="AE" isoCode="AE">
            <description>United Arab Emirates</description>
            <states>
                <state code="DU">
                    <name>UNITED ARAB EMIRATES</name>
                </state>
                <state code="FU">
                    <name>Fujairah</name>
                </state>
                <state code="AZ">
                    <name>Abu Dhabi</name>
                </state>
                <state code="AJ">
                    <name>Ajman</name>
                </state>
                <state code="07">
                    <name>UNITED ARAB EMIRATES</name>
                </state>
                <state code="XX">
                    <name>NOT ASSIGNED</name>
                </state>
                <state code="UQ">
                    <name>Umm Al Quwain</name>
                </state>
                <state code="RK">
                    <name>Ras Al Khaimah</name>
                </state>
                <state code="SH">
                    <name>Sharjah</name>
                </state>
            </states>
        </country>

但是,当我使用 for 循环显示带有 {% for country in response.CountriesRS.countries %} 的国家/地区时,我只能看到“国家/地区”正在显示。我怎样才能正确地循环这个字典? 这就是变量“响应”在 html 中的显示方式:

OrderedDict([('countriesRS', OrderedDict([('@xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'), ('@xmlns', 'http://www.hotelbeds.com/schemas/messages'), ('from', '1'), ('to', '203'), ('total', '207'), ('auditData', OrderedDict([('@processTime', '28'), ('@timestamp', '2020-10-01 21:09:13.151'), ('@requestHost', '10.185.89.196'), ('@serverId', 'ip-10-185-88-253.eu-west-1.compute.internal.node.int-hbg-aws-eu-west-1.discovery'), ('@environment', '[awseuwest1, awseuwest1a, ip_10_185_88_253]'), ('@release', 'cbd9f69ac1e7076b6ae86a8358f1e1bf26e00f44'), ('@internal', '23cbd9bfc8a64401824599cc99028cc0')])), ('countries', OrderedDict([('country', [OrderedDict([('@code', 'AD'), ('@isoCode', 'AD'), ('description', 'Andorra'), ('states', OrderedDict([('state', [OrderedDict([('@code', 'CA'), ('name', 'CANILLO')]), OrderedDict([('@code', 'EN'), ('name', 'ENCAMP')]), OrderedDict([('@code', 'ES'), ('name', 'ESCALDES - ENGORDANY')]), OrderedDict([('@code', 'AN'), ('name', 'ANDORRA')]), OrderedDict([('@code', 'AD'), ('name', 'ANDORRA LA VELLA')]), OrderedDict([('@code', '07'), ('name', 'ANDORRA')]), OrderedDict([('@code', 'XX'), ('name', 'NOT ASSIGNED')]), OrderedDict([('@code', 'OR'), ('name', 'ORDINO')]), OrderedDict([('@code', 'SJ'), ('name', 'SANT JULIA DE LORIA')]), OrderedDict([('@code', 'MS'), ('name', 'LA MASSANA')])])]))]), OrderedDict([('@code', 'AE'), ('@isoCode', 'AE'), ('description', 'United Arab Emirates'), ('states', OrderedDict([('state', [OrderedDict([('@code', 'DU'), ('name', 'UNITED ARAB EMIRATES')]), OrderedDict([('@code', 'FU'), ('name', 'Fujairah')]), OrderedDict([('@code', 'AZ'), ('name', 'Abu Dhabi')]), OrderedDict([('@code', 'AJ'), ('name', 'Ajman')]), OrderedDict([('@code', '07'), ('name', 'UNITED ARAB EMIRATES')])

【问题讨论】:

    标签: python django xml


    【解决方案1】:

    尝试将.country 添加到您的链中:

    {% for country in response.CountriesRS.countries.country %}
      <li>{{ country.description }}</li>
    {% endfor %}
    

    【讨论】:

      【解决方案2】:

      问题解决了。即使 country 是 xml 字符串中的国家/地区列表,当转换为 dict 时,“country”一词也不会重复,因此不会循环。使用 for country 作为响应。CountriesRS.countries.country 解决了这个问题。

      【讨论】:

        猜你喜欢
        • 2019-11-24
        • 2013-05-13
        • 1970-01-01
        • 2016-08-31
        • 2012-12-25
        • 2011-11-10
        • 2015-12-25
        • 2020-08-03
        • 1970-01-01
        相关资源
        最近更新 更多