data passing app in falcon python
Asked Answered
W

1

5

Before asking the question i want to mention that i am aware of the fact that i can use django instead to make the app, but i need to use falcon and nothing else.

i am just looking for an approach

lets take a very simple scenario so that i can understand how data flows between various parts of the app.

i have a simple login page using html:

<!DOCTYPE html>
<html>
<body>

<form action="***what-do-i-put-here***">
  <fieldset>
    <legend>Personal information:</legend>
    First name:<br>
    <input type="text" name="firstname" value="Mickey">
    <br>
    Last name:<br>
    <input type="text" name="lastname" value="Mouse">
    <br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

</body>
</html>

i run it using simpleHTTpServer present by default in python.

now i create a very basic falcon app with just one responder "on_post()" which just replies back with the data that it recieved from the form,

i am using uWsgi on localserver to host my falcon app. how do i the make these two different pieces of code to interact with each other i mean in the html form ,what we do in case of Php is we define the name of php file under "actions" tag .how do we do this in falcon.

a very simple and small working example is highly appreciated

Warford answered 5/1, 2016 at 18:20 Comment(0)
S
10

Here is a working example!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="http://127.0.0.1:8000" method="post">
        <input type="text" name="name">
        <button type="submit" name="btn">Submit</button>
    </form>
</body>
</html>

Falcon code:

import falcon
from wsgiref import simple_server

class Resource(object):
    def on_post(self, req, resp):
        resp.status = falcon.HTTP_200
        resp.body = req.params['name']

app = api = falcon.API()
app.req_options.auto_parse_form_urlencoded = True
api.add_route('/', Resource())

if __name__ == '__main__':
    http = simple_server.make_server('127.0.0.1', 8000, app)
    http.serve_forever()
Selfstyled answered 15/12, 2016 at 16:4 Comment(1)
Wow, it took me forever to find api.req_options.auto_parse_form_urlencoded = True. Thank you.Bleacher

© 2022 - 2024 — McMap. All rights reserved.