I have a view which returns a formatted JSON object. It takes a list of words (labeled "forms" and "lemmas" for this project), formats them into a nice dictionary object and outputs a json page which can then be saved by the user.
views.py
def search_view(request):
"""
The app revolves around this view, which is also the homepage.
"""
context = {}
if request.method == 'GET':
return render(request, 'search.html', context)
if request.method == 'POST':
checked_A = {}
checked_B = {}
# All lemma keys are of the form {lemma}@{group}.
# All form keys are of the form {lemma}@{group}@{form}.
# The "@" symbol will not appear in any other key.
# Split the key_data into two parts, the lemma/group part
# and the form part. The form part is either [] or [form]
for key in request.POST:
if "@" not in key:
continue
key_data = key.split("@")
(lemma, group), form = key_data[:2], key_data[2:]
if group == "A":
if checked_A.get(lemma):
checked_A[lemma].extend(form)
else:
checked_A[lemma] = list(form)
if group == "B":
if checked_B.get(lemma):
checked_B[lemma].extend(form)
else:
checked_B[lemma] = list(form)
context['query_A'] = checked_A
context['query_B'] = checked_B
return JsonResponse(context, status=200) # <-- the only important part
Is there a way to create a button which will download this data directly, instead of having the user go to a separate page and save the data?