omer bach
omer bach

Reputation: 2405

Python bottle form response handling

I'm using bottle as a web server in my application. I have a scenario in which an html at the client side has a form which its action is : "/updateDb"

    <!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js'></script>

<form action="/updateData" method="post" id="inputFrm">
<input type="button" id="submitBtn" value="submit"/>
<input name ="studentId" type="text"/>
</form>




<script>
$(document).ready(function() { 


$("#submitBtn").click(function()
            {   
            document.forms["inputFrm"].submit();
            });
            });
</script>
</body>
</html>

In the server side I'm inserting the student id to the database and then wish to update the response object according to the current status. for example, if the insertion to the db failed, i'd like to return a response object with some descriptive text and status and decide on the client side how to act.

So my question is : where in the python code can I deal with bottle's response object which represents the from's response?

Thanks

Upvotes: 2

Views: 2527

Answers (1)

drnextgis
drnextgis

Reputation: 2224

For example:

from bottle import get
from bottle import post
from bottle import request
from bottle import Bottle
from bottle import run

app = Bottle()

@app.get('/updateData')
def login_form():
    return '''<form method="POST" action="/updateData">
                <input name="name"     type="text" />
                <input type="submit" />
              </form>'''

@app.post('/updateData')
def submit_form():
    name = request.forms.get('name')
    if name != 'omer bach':
        return dict(succes=True, desc='This name is not presented in database')
    else:
        return dict(success=False, desc='This name is already in database. Choose another one.')

run(app, host='0.0.0.0', port=8000)

Upvotes: 4

Related Questions