如何将列表转换为html using a for loop in django template

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

这是我的第一篇文章,我刚刚开始使用python和django。我已成功连接到公共(未经过身份验证)的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)']

我一直关注:https://simpleisbetterthancomplex.com/tutorial/2018/02/03/how-to-use-restful-apis-with-django.html并尽可能多地搜索,以帮助我更好地理解如何访问列表元素并迭代它们。

模板html呈现以上内容:

    {% 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})
python django api
2个回答
0
投票

你快到了!

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

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

views.朋友

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

0
投票

有可能当你使用我们的朋友之前描述的get函数时,它仍然将字段作为整个字符串,在这种情况下你可以用这个解决它:

roles_list = search_result.get('add_titles').strip(']').strip('[').replace("'", '').split(",")

让我们知道怎么回事。

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