1

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?

2
  • What does your template code look like. Also, why do you want to perform this check on the client side (i.e. using javascript)? And what do you plan to do once you find out if an institution exceeds 100k or not? Commented Oct 28, 2011 at 17:15
  • @ladaghini I need to do it on the fly as they select the institution, thats why I am using javascript. Commented Oct 28, 2011 at 17:19

1 Answer 1

1

I don't know what you intend to do with the fi_list variable 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 the amount data 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_list into a JSON string, make your response have (in a script tag) code like:

var finInst = jQuery.parseJSON( "{{ jsonString }}" ); function checkLimit(amount) { // I don't know what amount is supposed to do. if (finInst[jQuery(this).text()] > 100000) // do amount > 100000 code else // amount within limit } 

With django/python code like:

import json if plan: investments = Investment.objects.all().filter(plan = plan).order_by('maturity_date').filter(maturity_date__gte = now) fi_list = {} for i in investments: financial_institution = i.financial_institution amount = i.get_current_value(date) fi_list[financial_institution] = amount context['jsonString'] = json.dumps(fi_list) 

And finally, whenever an institution option is selected from the web page, trigger the checkLimit function.

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.