I need to loop through my django list that I have passed to the template.
I have this code in my django view:
if plan:
investments = Investment.objects.all().filter(plan = plan).order_by('maturity_date').filter(maturity_date__gte = now)
for i in investments:
financial_institution = i.financial_institution
amount = i.get_current_value(date)
fi_list.append({
'fi': financial_institution,
'amt':amount
})
context['list'] = fi_list
Which outputs:
[<financial_institution: Example> <amount: 5000>]
Now what I want to do is loop through this list, and if my javascript variable matches the item in the list, do further code. However I am stuck on how to do this.
Here is my javascript so far, using jQuery:
function cdic_limit(amount) {
var limit = 100000.00;
var list ="{{ list }}";
var fi = $("#id_financial_institution option:selected").text();
}
Down the road, what I ultimately want, is if the selected institution is in the list, check and make sure their total amount isn’t exceeding $100k
Any suggestions?
I don’t know what you intend to do with the
fi_listvariable that you add to the context. If you plan to list the institutions and their limits in a systematic way, such as a table, then it should be simple enough to retrieve theamountdata in much the same way as you retrieved the selected financial institution’s name.If you intend to reveal to the user the amounts for all the institutions (which I don’t think you do), and by reveal I mean it exists anywhere in the HTML code, whether or not the browser renders it, then one thing you can do is encode
fi_listinto a JSON string, make your response have (in a script tag) code like:With django/python code like:
And finally, whenever an institution option is selected from the web page, trigger the
checkLimitfunction.Honestly, this is really bad code because I think you don’t want to expose all these amount values for each institution (critically confidential info maybe?). So the only reliable way to produce on the fly results would be to use AJAX to call a django view whenever an institution is selected. You might want to look at dajaxproject to simplify these requests.