david419
david419

Reputation: 435

html post values addition in bottle application

I was trying to add two html form values transferred via post in a python bottle application. Unfortunately it is just concatenating. I tried to convert the inputs to int but then i get" Unhandled Exception Error".... This is my code

from bottle import get, post, request,default_app # or route
import math

@get('/login') # or @route('/login')
def login():
    return '''
        <form action="/login" method="post">
            Number1: <input name="num1" type="number" />
            Number2: <input name="num2" type="number" />
            <input value="Add" type="submit" />
        </form>
    '''

@post('/login') # or @route('/login', method='POST')
def do_login():

    num1 = request.forms.get('num1')
    num2 = request.forms.get('num2')

    return num1+num2
import os
from bottle import TEMPLATE_PATH
TEMPLATE_PATH.append(os.path.join(os.environ['OPENSHIFT_HOMEDIR'], 
    'runtime/repo/wsgi/views/')) 
application=default_app()

If i type return (int)num1+(int)num2 unhandled exception error results.. Dont know why.. Same is for type(),float() functions as well.

Upvotes: 1

Views: 507

Answers (2)

zgyarmati
zgyarmati

Reputation: 1153

I think you just have to convert the return value to str, so try:

return str(int(num1) + int(num2))

Upvotes: 1

user2737086
user2737086

Reputation: 299

Try:

return int(num1)+int(num2)

and not:

return (int)num1+(int)num2

Upvotes: 0

Related Questions