Python 如何将列表转换为html<;ul>;在django模板中使用for循环

Python 如何将列表转换为html<;ul>;在django模板中使用for循环,python,django,api,Python,Django,Api,这是我的第一篇文章,我刚刚开始学习python和django。我已成功连接到公共(未经身份验证)API。当我显示结果时-我可以访问所有字段,但其中一个字段作为列表返回-其他角色。我可以显示整个列表(未格式化),以逗号分隔-但我不知道如何迭代列表并将其呈现为 返回的列表如下所示: SOC: 3112 Title: Electrical and electronics technicians Description: Electrical and electronics technicians

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

返回的列表如下所示:

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)'] 在项目符号列表中,我尝试了几种不同的选项,包括:

        <ul>
          {% for title in search_result.add_titles %}
              <li>{{ title }}</li>
          {% endfor %}
        </ul> 
你快到了

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

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

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()
        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 %}
{%if search\u result.success%}

SOC:{{search_result.SOC}

标题:{{search\u result.Title}
说明:{{search\u result.Description}
资格:{{search\u result.Qualifications}
任务:{{search\u result.Tasks}
其他角色:
    {其他_角色中的角色为%}
  • {{role}}
  • {%endfor%}

{%else%} {{search_result.message}

{%endif%}
你就快到了

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

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

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()
        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 %}
{%if search\u result.success%}

SOC:{{search_result.SOC}

标题:{{search\u result.Title}
说明:{{search\u result.Description}
资格:{{search\u result.Qualifications}
任务:{{search\u result.Tasks}
其他角色:
    {其他_角色中的角色为%}
  • {{role}}
  • {%endfor%}

{%else%} {{search_result.message}

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

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

让我们知道它是如何运行的。

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

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

让我们知道它是如何运行的。

发生的事情是,它将其他标题列表作为一个完整的字符串,您需要首先从视图中将其转换为列表,你能发布你的视图让我帮你吗?@jsanchezs谢谢你看一看-我已经添加了视图-我已经尝试提取了
add_titles
,但不确定如何使用返回它们。
  • {{title}
  • 打印了什么?@tobyt99很棒,检查下面的两个答案并让我们知道=)发生了什么事情它将其他标题列表作为一个完整的字符串,您需要首先从视图中将其转换为列表,你能发布你的视图让我帮你吗?@jsanchezs谢谢你看一看-我已经添加了视图-我已经尝试提取了
    add_titles
    ,但不确定如何使用它们。什么是
    {{{title}{code>打印的?@tobyt99太好了,检查下面的两个答案并让我们知道=)这几乎奏效了。。。非常感谢。事实证明,我需要在views.py中的if语句之前创建变量
    other_roles
    ——之后,一切正常。我觉得我在[]和(-)帮助之间很迷茫。如果你因为我的回答而解决了这个问题,你可以考虑投票或接受答案。这几乎起作用了。非常感谢。事实证明,我需要在views.py中的if语句之前创建变量
    other_roles
    ——之后,一切正常。我觉得我在[]和(-)帮助之间很迷茫。如果你因为我的回答而解决了这个问题,你可以考虑赞成或接受这个答案。