hellomello hellomello - 7 months ago 35
Python Question

python-django: adding math in my views.py?

Do I need to import some kind of module to do math in my views.py?

def calc(request):
if 'pv' in request.GET and 'r' in request.GET:
pv = request.GET['pv']
r = request.GET['r']

calcPV = pv * r
return render(request, 'main/index.html',
'pv': pv,
'r': r,
'calcPV': calcPV,
return render(request, 'main/index.html')

For example purposes, I'm trying to do a simple multiplication:
calcPV = pv * r
but its not working. Am I doing this wrong?



You don't need to import any module to do a multiplication.

You might have to cast your parameters first. For example:

pv = int(request.GET['pv'])

In the HTML, you can show the results like this:

{{ calcPV }}