Adding POST values in python bottle application

This is the place for queries that don't fit in any of the other categories.

Adding POST values in python bottle application

Postby david419 » Sat Oct 12, 2013 6:53 pm

I am trying to add two values in python bottle application but it is concatenating. If i use int() function to convert the values to integer it results in "Unhandled Exception" Error. The same if i use functions like type(), float(), etc.. Below is the code which is concatenating the values..


Code: Select all
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

# This must be added in order to do correct path lookups for the views
import os
from bottle import TEMPLATE_PATH
TEMPLATE_PATH.append(os.path.join(os.environ['OPENSHIFT_HOMEDIR'],
    'runtime/repo/wsgi/views/'))

application=default_app()


I have tried return int(num1)+int(num2) but it return "Unhandled Exception" Error as i said.. Dunno what to do.....
david419
 
Posts: 2
Joined: Sat Oct 12, 2013 6:46 pm

Re: Adding POST values in python bottle application

Postby micseydel » Sat Oct 12, 2013 8:27 pm

Your routes need to return strings, not ints (maybe other things too but definitely not ints). Your last line of do_login() should be
Code: Select all
return str(int(num1)+int(num2))
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1393
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Adding POST values in python bottle application

Postby david419 » Sun Oct 13, 2013 6:09 am

Thanks a lot... :D Worked like a charm...
david419
 
Posts: 2
Joined: Sat Oct 12, 2013 6:46 pm

Re: Adding POST values in python bottle application

Postby micseydel » Sun Oct 13, 2013 5:44 pm

Happy to help!
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1393
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Baldyr and 1 guest