Django for-loop to calculate loan repyament and interest for a period only displaying the last iteration

I am building a loan system to calculate the repayment period. am having this issue when i run the code it only display the last row. below is my view.py please i need it to display all the rows not only the last row

def sveloan(request):
    if 'userid' in request.session:
        if request.method == 'POST':
                            
              balance = 120000

              annual_interest_rate = 42

              monthly_payment_rate =0.3

              monthly_interest_rate = annual_interest_rate / 12

              monthly_payment = monthly_payment_rate * balance

              new_balance= (balance - monthly_payment) * (1 + monthly_interest_rate)

              for month in range(1, 6):

                  monthly_payment = 20000

                  balance = (balance - monthly_payment)

                  rateint = (balance / 100 * monthly_interest_rate)

                  mydist = ('Month: %d \n Minimum monthly payment: %g \n Remaining balance: %g \n Minimum Interest payment: %g'\
                      % (month, round(monthly_payment, 2), round(balance,2), round(rateint, 2)))


                  context={
                      'mylist': mydist
                  }
                      
              
              return render(request,'loantest.html',context)

              
        else:
            return render(request,'index.html')
           
    else:
        return render(request,'index.html')

In my template:

{% for p in mylist %}

{{p}}

{% endfor %}

I think you've just got a couple small things that need to be adjusted. Here's my quick attempt at repairing that code. I've added explanation comments inline:

def sveloan(request):
    if 'userid' in request.session:
        if request.method == 'POST':

            balance = 120000

            annual_interest_rate = 42

            monthly_payment_rate = 0.3

            monthly_interest_rate = annual_interest_rate / 12

            monthly_payment = monthly_payment_rate * balance

            new_balance = (balance - monthly_payment) * (1 + monthly_interest_rate)

            # setting up a collector here for the individual mydist tuples/rows
            mylist = []

            for month in range(1, 6):
                monthly_payment = 20000

                balance = (balance - monthly_payment)

                rateint = (balance / 100 * monthly_interest_rate)

                mydist = (
                        'Month: %d \n Minimum monthly payment: %g \n Remaining balance: %g \n Minimum Interest payment: %g' \
                        % (month, round(monthly_payment, 2), round(balance, 2), round(rateint, 2)))

                # add each mydist tuple to the mylist collector
                mylist.append(mydist)

            # de-indenting this, it should not be within your for loop
            context = {
                # use the mylist value, which now has all of the rows
                'mylist': mylist
            }

            return render(request, 'loantest.html', context)


        else:
            return render(request, 'index.html')

    else:
        return render(request, 'index.html')

Your for loop is overwriting mylist every iteration of the for loop and therefore only capturing the last iteration.

To solve this you could append the mydist variable to python list declared outside the loop and append to it every iteration. Then, pass that list to the context after the loop is finished. For example:

mylist = []

for month in range(1, 6):
    # whatever code
    mydist = 'whatever value'
    mylist.append(mydist)

context = {
    'mylist': mydist
}


Back to Top