gpt4 book ai didi

python - 如何在 django 模板中使用 for 循环将列表转换为 html

    转载 作者:太空宇宙 更新时间:2023-11-03 20:58:26 26 4
    gpt4 key购买 nike

    这是我的第一篇文章,我刚刚开始使用 python 和 django。我已成功连接到公共(public)(未经身份验证)API。当我显示结果时 - 我可以访问所有字段,但其中一个字段会作为列表返回 - 其他角色。我可以显示整个列表(未格式化),以逗号分隔 - 但我不知道如何迭代列表并将其呈现为 <ul> .

    返回的列表如下所示:

    SOC: 3112 Title: Electrical and electronics technicians Description: Electrical and electronics technicians perform a variety of miscellaneous technical support functions to assist with the design, development, installation, operation and maintenance of electrical and electronic systems. Qualifications: Entrants usually possess GCSEs/S grades, an Intermediate GNVQ/GSVQ Level II or a BTEC/ SQA award. NVQs/SVQs in Servicing Electronic Systems are available at Levels 2 and 3. Tasks: plans and prepares work and test schedules based on specifications and drawings; sets up equipment, undertakes tests, takes readings, performs calculations and records and interprets data; plans installation methods, checks completed installation for safety and controls or undertakes the initial running of the new electrical or electronic equipment or system; diagnoses and detects faults and implements procedures to maintain efficient operation of systems and equipment; visits and advises clients on the use and servicing of electrical and electronic systems and equipment. Other roles: ['Assistant, electronics', 'Engineer, executive (telecommunications)', 'Technician, electronics', 'Officer, signals (MOD)', 'Specialist, telecommunications', 'Technician, electrical', 'Engineer, assistant (broadcasting)', 'Engineer, simulator, flight', 'Technician, telemetry', 'Engineer, testing, cable, assistant', 'Technician, maintenance, electrical', 'Technician', 'Technician, avionics', 'Engineer, installation (electricity supplier)']

    I have been following: https://simpleisbetterthancomplex.com/tutorial/2018/02/03/how-to-use-restful-apis-with-django.html and trawled through as much as I can find to help me better understand how to access the list elements and iterate over them.

    The template html renders the above:

        {% if search_result.success %}
    <p>
    <strong>SOC:</strong> {{ search_result.soc }}
    <br />
    <strong>Title:</strong> {{ search_result.title }}
    <br />
    <strong>Description:</strong> {{ search_result.description }}
    <br />
    <strong>Qualifications:</strong> {{ search_result.qualifications }}
    <br />
    <strong>Tasks:</strong> {{ search_result.tasks }}
    <br />
    <strong>Other roles:</strong> {{ search_result.add_titles }}
    </p>
    {% else %}
    <p><em>{{ search_result.message }}</em></p>
    {% endif %}

    努力扭转最终局面

    {{ search_reults.add_titles }}

    在项目符号列表中,我尝试了几种不同的选项,包括:

            <ul>
    {% for title in search_result.add_titles %}
    <li>{{ title }}</li>
    {% endfor %}
    </ul>

    我希望将列表变成这样:

    • 电子助理
    • 工程师、主管(电信)
    • 电子技术员
    • 信号警官 (MOD)
    • 电信专家
    • 电气技术员
    • 工程师、助理(广播)
    • 工程师、模拟器、飞行
    • 技术员,遥测
    • 工程师、测试、电缆、助理
    • 维修、电气技术员
    • 技术员
    • 航空电子技术员
    • 安装工程师(电力供应商)

    任何帮助将不胜感激 - 希望是新手错误?

    编辑:

    当前views.py:

    def lmi4all(request):
    search_result = {}
    if 'SOC' in request.GET:
    soc = request.GET['SOC']
    url = 'http://api.lmiforall.org.uk/api/v1/soc/code/%s' % soc
    response = requests.get(url)
    search_was_successful = (response.status_code == 200) # 200 = SUCCESS
    search_result = response.json()
    search_result['success'] = search_was_successful

    return render(request, 'core/lmi4all.html', {'search_result': search_result})

    最佳答案

    你就快到了!

    您可以做的是将 add_titles 存储在 View 中的变量中(它将创建一个包含每个作业的列表),然后只需在上下文中添加此列表即可。

    然后从模板中您可以使用它:

    View .py

    def lmi4all(request):
    search_result = {}
    if 'SOC' in request.GET:
    soc = request.GET['SOC']
    url = 'http://api.lmiforall.org.uk/api/v1/soc/code/%s' % soc
    response = requests.get(url)
    search_was_successful = (response.status_code == 200) # 200 = SUCCESS
    search_result = response.json()
    other_roles = search_result.get('add_titles')
    search_result['success'] = search_was_successful

    return render(request, 'core/lmi4all.html', {'search_result': search_result, 'other_roles': other_roles})

    模板:

    {% if search_result.success %}
    <p>
    <strong>SOC:</strong> {{ search_result.soc }}
    <br />
    <strong>Title:</strong> {{ search_result.title }}
    <br />
    <strong>Description:</strong> {{ search_result.description }}
    <br />
    <strong>Qualifications:</strong> {{ search_result.qualifications }}
    <br />
    <strong>Tasks:</strong> {{ search_result.tasks }}
    <br />
    <strong>Other roles:</strong>
    <ul>
    {% for role in other_roles %}
    <li>
    {{role}}
    </li>
    {% endfor %}
    </ul>
    </p>
    {% else %}
    <p><em>{{ search_result.message }}</em></p>
    {% endif %}

    关于python - 如何在 django 模板中使用 for 循环将列表转换为 html <ul>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55857938/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com