Django - Return only checked object from nested JSON
I have a problem returning checked data from html page views.py: First I open JSON file and store it to data
from django.shortcuts import render
from django.http import HttpResponse, request
import json
from fpdf import FPDF
from django.db import models
# Create your views here.
#invalid = '<>:"//\t\|?*'
url = 'podaci.json'
class PDF(FPDF):
def header(self):
self.image('Logo_SAVA_osiguranje_RGB.png',w=60)
self.ln(20)
with open(url,encoding="utf8") as f:
data=json.load(f)
def remove(string, i):
a = string[ : i]
b = string[i + 1: ]
return a + b
def display_data(request):
render(request,'home.html',{'data':data})
if request.method=='POST':
for podatak in request.POST.getlist('checks'):
print(podatak)
return render(request,'home.html',{'data':data})
html: in HTML i set a value of Checkbox to be an object of data
<body class="container">
<form method="post"> {% csrf_token %}
<button type="submit">Submit</button>
{% for duznici in data %}
<div class="card">
<div class="card-header" >
{{duznici.NazivDuznika}}
<input class="form-check-input" type="checkbox" name="checks" value='{{data}}'>
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
{% for podaci in duznici.DunningItems %}
<p name="checks" value="{{NazivDuznika}}">{{podaci.BrojPolise}}</p>
{% endfor %}
</blockquote>
</div>
</div>
{% endfor %}
</form>
</body>
podaci.json structure:
[
{
"NazivDuznika": "xxxxxxxx",
"Pib": "xxxxxxxx",
"Mtbr": "xxxxxxxx",
"IdLica": xxxxxxxx,
"Email": xxxxxxxx,
"DunningItems": [
{
"BrojPolise": "xxxxxxxx",
"Broj_dokumenta_Z": "xxxxxxxx",
"Dug": xxxxxxxx,
"Datum_Dospeca_Z": "xxxxxxxx",
"RbRate": xxxxxxxx,
"VrstaOsiguranja": "xxxxxxxx",
"Rocnost": xxxxxxxx
},
{
"BrojPolise": "xxxxxxxx",
"Broj_dokumenta_Z": "xxxxxxxx",
"Dug": xxxxxxxx,
"Datum_Dospeca_Z": "xxxxxxxx",
"RbRate": xxxxxxxx,
"VrstaOsiguranja": "xxxxxxxx",
"Rocnost": xxxxxxxx
},
{
"BrojPolise": "xxxxxxxx",
"Broj_dokumenta_Z": "xxxxxxxx",
"Dug": xxxxxxxx,
"Datum_Dospeca_Z": "xxxxxxxx",
"RbRate": xxxxxxxx,
"VrstaOsiguranja": "xxxxxxxx",
"Rocnost": xxxxxxxx
}
]
},
{
"NazivDuznika": "xxxxxxxx",
"Pib": "xxxxxxxx",
"Mtbr": "xxxxxxxx",
"IdLica": xxxxxxxx,
"Email": xxxxxxxx,
"DunningItems": [
{
"BrojPolise": "xxxxxxxx",
"Broj_dokumenta_Z": "xxxxxxxx",
"Dug": xxxxxxxx,
"Datum_Dospeca_Z": "xxxxxxxx",
"RbRate": xxxxxxxx,
"VrstaOsiguranja": "xxxxxxxx",
"Rocnost": xxxxxxxx
}
]
}]
data i get from is string but i need it to keep json form so i can iterate through the JSON and generate PDF document later so i can save pdf files and send them via email later
request.POST.getlist('checks')