Name Error in Django - Nested Function in Views Function - Saving to Database

283 Views Asked by At

Using Django, While trying to save the answers from the User to the Database, it is showing me a Name Error. I have nested a function inside a views function, it works correctly when i do not attempt to save the data to the database. But once i try to put in code inside the views function to save the data, it produces this name error. Below are the files with the code,

models.py file

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User 

class ansquestions(models.Model):
    m_invested = models.CharField(max_length=100)
    p_return = models.CharField(max_length=100)
    years = models.CharField(max_length=100)
    inflation_yes_no = models.CharField(max_length=100)
    r_output = models.TextField(default=True)
    date_answered = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

forms.py file

from django import forms
from .models import ansquestions

class ansquestionsForm(forms.ModelForm):
    class Meta:
        model = ansquestions
        fields = ['m_invested', 'p_return', 'years', 'inflation_yes_no', 'r_output', 'date_answered', 'author'

]

views.py file (Note : the fvinvestment function is nested under investmentfvcalc function. when pasting in here, its not showing as indented under the investmentfvcalc, but in my real code the fvinvestment function is indented and nested under the investmetncalc function. )

from django.shortcuts import render
#from django.http import HttpResponse
from .models import ansquestions
from .forms import ansquestionsForm


def investmentfv(request):
    idata = { 'tmi':tmi, 'pry':pry, 'ys':ys, 'qinf':qinf }
    return render(request, 'fvalueapp/investmentfv.html', idata)




 def investmentfvcalc(request):
    if request.method == 'POST':
        form = ansquestionsForm(request.POST)
        if form.is_valid():
            total_i = request.POST.get["m_invested", '']
            perc_r = request.POST.get["p_return", '']
            years_i = request.POST.get["years", '']
            makeup_infl = request.POST.get["inflation_yes_no", '']
            ansquestions_obj = ansquestions(m_invested=total_i, p_return=perc_r, years=years_i, inflation_yes_no=makeup_infl)
            ansquestions_obj.save() 

            

def fvinvestment():
        global fv_i_value
        global r_output
        #global makeup_infl ( global variable might be needed for technique for above. as error says makeup_inf is not defined ) 
        if makeup_infl=='no':
            i_return = (float(perc_r))
        elif makeup_infl=='yes' and int(years_i)<=5:
            i_return = (2+float(perc_r))
        elif makeup_infl=='yes' and int(years_i)>5 and int(years_i)<=9.99 :
            i_return = (4 + float(perc_r))
        elif makeup_infl=='yes' and int(years_i)>= 10 :    
            i_return = ( 6 + float(perc_r))
                
        
        fv_i_value = int(total_i) * (1 + (i_return)*.01) ** int(years_i)

        r_output = 'Your money would grow to the total of ' + str(fv_i_value) +' dollars at the end of this timeframe.'                     
        
    
    fvinvestment()   


  

  

    
idata = { 'tmi':tmi, 'pry':pry, 'ys':ys, 'qinf':qinf, 'r_output': r_output }
return render (request, 'fvalueapp/fvinvestmentcalcresults.html', idata)

Errors :

Running the above code it gives me the error "NameError at investmentfv/Investementfvcalc" "free variable 'makeup_infl' referenced before assignment in enclosing scope " i than later would put the variable "makeup_infl" as a Global variable in the fvinvestment function, it than would produce error "NameError - name 'makeup_infl' is not define

1

There are 1 best solutions below

2
On

Change fvinvestment to global scope, pass in these variables: years_i and makeup_infl

def fvinvestment(years_i, makeup_infl):
    ....

This will also solve your makeup_infl not defined error