Это мой первый пост, я только начинаю с 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)
- Специалист, телекоммуникации
- Техник, электрик
- Инженер, помощник (вещание)
- Инженер, симулятор, полет
- Техник, телеметрия
- Инженер, тестировщик, кабель, помощник
- Техник, техобслуживание, электрика
- Техника
- Техник, авионика
- Инженер, монтаж (поставщик электроэнергии)
Любая помощь будет очень признательна - надеюсь, ошибка новичка?
EDIT:
текущие 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})