I had a look at your question.
First, two things that were already noted in the comments:
- Get rid of the global variables. Global variables will behave in unexpected?ways in a Django application, you should not use them unless you really, really, really know what you are doing.
- Capitalize your class names, i.e. Ansquestions and Results
With that out of the way. Let's try to figure out what's going on here. My understanding is that you want to collect some information about an investment and then calculate the returns.
Why is Results a separate model? It could be a field on your Ansquestions. You could store the string you want to display, or even better, just the numerical value.
Why is?formulafv a separate view? You can do the calculation immediately when you receive the inputs.
results.objects.latest('id')
will indeed give you the latest object. But once you have multiple users accessing your application, you will run into a race-conditions so you get the results from a different user. But you don't really need it here anyways.
Don't use request.POST directly, use form.cleaned_data. If your?AnsquestionsForm uses FloatFields and IntegerFields, form.cleaned_data will contain floats and ints. You then don't need any conversions from str to int in?formulafv
Here is some (untested) code to illustrate what I mean:
# models.py
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)
? ? date_answered = models.DateTimeField(default=timezone.now)
? ? author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
? ? results = models.FloatField()
? ? @property
? ? def investment_return(self):
? ? ? ? return 'Your money would grow to the total of {} dollars at the end of this timeframe.'.format(self.results)
# views.py
def investmentfv(request):
? ? idata = { 'tmi':tmi, 'pry':pry, 'ys':ys, 'qinf':qinf }
? ? if request.method == "POST":
? ? ? ? form = AnsquestionsForm(request.POST or None)
? ? ? ? if form.is_valid():
? ? ? ? ? ? total_i = form.cleaned_data['m_invested']
? ? ? ? ? ? perc_r = form.cleaned_data["p_return"]
? ? ? ? ? ? years_i = form.cleaned_data["years"]
? ? ? ? ? ? makeup_infl = form.cleaned_data["inflation_yes_no"]
? ? ? ? ? ? results = formulafv(makeup_infl, years_i, perc_r, total_i)
? ? ? ? ? ? newdata=ansquestions(m_invested=total_i, p_return=perc_r, years=years_i, inflation_yes_no=makeup_infl, results=results)
? ? ? ? ? ? newdata.author = request.user ? ? ? ?
? ? ? ? ? ? newdata.save()
? ? ? ? ? ? return redirect('formulafv')# redirects to formulafv view function below. 'formulafv' is the name variable for that url pattern.
? ? else:
? ? ? ? return render(request, 'fvalueapp/investmentfv.html', idata)
? ?
? ? idata = { 'tmi':tmi, 'pry':pry, 'ys':ys, 'qinf':qinf, 'form': form }
? ? return render(request, 'fvalueapp/investmentfv.html', idata)
def formulafv(makeup_infl, years_i, perc_r, total_i):
? ? if makeup_infl=='no':
? ? ? ? i_return = perc_r
? ? elif makeup_infl=='yes' and years_i<=5:
? ? ? ? i_return = 2 + perc_r
? ? elif makeup_infl=='yes' and years_i>5 and years_i<=9.99 :
? ? ? ? i_return = 4 + perc_r
? ? elif makeup_infl=='yes' and years_i>= 10 :
? ? ? ? i_return = 6 + perc_r
? ? fv_i_value = total_i * (1 + (i_return)*.01) ** years_i
? ? return fv_i_value
# admin.py
class AnsquestionAdmin(admin.ModelAdmin):
? ? list_display = [..., 'investment_return', ...]
Also, I would suggest using the same name for a field/variable in your model, form and views. That makes it much easier to understand what's going on. I have not done that in the code above to not change everything completely.
I hope that points you in the right direction.