views.py
from django.shortcuts import render, redirect
import wikipedia
from wikipedia.exceptions import DisambiguationError, PageError
def index(request):
context = {}
if request.GET.get('search'):
search_value = wikipedia.search(request.GET.get('search'), results=3)
print(search_value)
keys = search_value
values = []
for s in search_value:
print(s)
try:
if wikipedia.summary(s):
values.append(wikipedia.summary(s))
except DisambiguationError as error:
print('errorrrrrr \n', error)
values.append("Disambiguation error")
except PageError as error:
print('erorrrrrrr \n', error)
values.append("Page error")
for i in range(len(keys)):
context[keys[i]] = values[i]
context = dict(zip(keys, values))
res = not bool(context)
print(res)
for key, value in context.items():
print('key', key)
print('value', value)
return render(request, 'webapp/index.html', context)
How to render these key value pairs in the template, is it related to this https://code.djangoproject.com/ticket/16335 if so can someone suggest a solution
index.html
<div class="results">
{% for key, value in context.items %}
<h3>something</h3>
{{key}}
{{value}}
{% endfor %}
</div>
The above for loop doesn't put anything in the template, but the same for loop in the views.py prints key value pairs in the terminal