[Answered ]-Preserve and display Django form inputs on POST submission

1👍

Two ways you can go about doing what you want as I see it:

Method 1
Use sessions to hold the data and then refeed it to the form using initial form values. This is probably the easiest since it most closely matches what you already have.

class Index(View):

    def get(self, request):
        
        # Fill in the initial values on the unbound form:
        initial = {
            'starting_amount': request.session.get('starting_amount'),
            'number_of_years': request.session.get('number_of_years'),
            'return_rate': request.session.get('return_rate'),
            'annual_additional_contribution': request.session.get('annual_additional_contribution'),
        }
        
        form = InvestmentForm(initial=initial)

        return render(request, 'home/index.html', {'form': form})


    def post(self, request):

        form = InvestmentForm(request.POST)

        if form.is_valid():

                # After getting the cleaned_data, set a session variable to hold it
                total_result = form.cleaned_data['starting_amount']
                request.session['starting_amount'] = total_result

                number_of_years = int(form.cleaned_data['number_of_years'])
                request.session['number_of_years'] = number_of_years

                return_rate = form.cleaned_data['return_rate']
                request.session['return_rate'] = return_rate

                annual_additional_contribution = form.cleaned_data['annual_additional_contribution']
                request.session['annual_additional_contribution'] = annual_additional_contribution
                
                total_interest = 0
                yearly_results = {}

                for i in range(1, number_of_years +1):
                    yearly_results[i] = {}

                    # CALCULATE THE INTEREST 
                    interest = total_result * (return_rate / 100)
                    total_result += interest 
                    total_interest += interest

                    # ADDITIONAL CONTRIBUTION 
                    total_result += annual_additional_contribution

                    # SET YEARLY RESULTS 
                    yearly_results[i]['interest']  = round(total_interest, 2)
                    yearly_results[i]['total'] = round(total_result,2)

                    # CREATE CONTEXT 
                    context = {
                        # CHANGED: pass the 'form' to the context!
                        'form': form,
                        'total_result': round(total_result,2),
                        'yearly_results': yearly_results,
                        'number_of_years': number_of_years
                    }
            
                # RENDER THE TEMPLATE

                return render(request, 'home/index.html', context)
                
        
        else:
            form = InvestmentForm()
            return render(request, 'home/index.html', {'form':form})

Method 2
From what you show here, anyway, you could just use JavaScript to do the calculations by grabbing the values directly from the DOM after they are entered, doing the calculations on the front end with JavaScript, and then fill in the values by targeting the locations where you want the results displayed in the DOM. No need for forms, no reloading of pages.

0👍

When you create the context, the value ‘form’ doesn’t get set (views.py, under get())

Leave a comment