Hi,
I have a form which submits three values through a function and I’m trying to get those results printed on the same html page where the form is. I can print the results in the terminal but not on the page.
Forms, Views, and HTML below. Thanks!
Forms:
from django import forms
class CalculatorForm(forms.Form):
value1 = forms.FloatField(required=False)
value2 = forms.FloatField(required=False)
value3 = forms.IntegerField(required=False)
Views:
class CalculatorView(TemplateView):
template= 'calculator/calculator.html'
def get(self, request):
form_c = CalculatorForm(prefix='form_c')
return render(request, self.template, {
'form_c': form_c,
})
def post(self, request):
form_c = CalculatorForm(request.POST, prefix='form_c')
try:
if form_c.is_valid():
post = form_c.cleaned_data
value1 = post.get('value1')
value2 = post.get('value2')
value3 = post.get('value3')
df1 = my_function(value1, value2, value3) # I want to print the results of this function on the page
print(df1)
except:
pass
args = {
'form_c': form_c,
}
return render(request, self.template, args)
HTML:
{% extends 'base.html' %}
<h1>Home Page</h1>
{% block head %}
<title>Check</title>
{% endblock %}
{% block body %}
<div class="container">
<h3>Calculator</h3>
<form method="post">
{% csrf_token %}
{{ form_c.as_p }}
<button type="submit">Submit</button>
</div>
<div class="container">
# my_function results here <---
{% endblock %}