如何在Django temp中使用for循环将列表转换为html

2024-05-16 10:03:19 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一篇文章,我刚刚开始学习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> 

我希望把清单变成这样:

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

任何帮助将不胜感激-希望是新手的错误?你知道吗

编辑:

电流视图.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})

Tags: andofthebr列表searchresultelectrical
2条回答

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

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

告诉我们进展如何。你知道吗

你就快到了!你知道吗

您可以做的是将add\u标题存储在视图的一个变量中(它将创建一个包含每个作业的列表),然后简单地将这个列表添加到上下文中。你知道吗

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

你知道吗视图.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 %}

相关问题 更多 >