shantanuo
shantanuo

Reputation: 32286

processing form inputs in python

This basic form is working as expected in python. It displays the contents of user submitted data to browser. What I need to do is pass this text string to pika to be sent to rabbitmq.

templates/hello_form1.html

<html>
<body>

<h1>Submit the Form</h1>

<form action="/hello" method="POST">
    file to download: <input type="text" name="greet">
    <br/>
    <input type="submit">
</form>

</body>
</html>

templates/index.html

$def with (greeting)

$if greeting:
    I just wanted to say <em style="color: green; font-size: 2em;">$greeting</em>.
$else:
    <em>Hello</em>, world!

cat app2.py

import web

urls = (
  '/hello', 'Index'
)

app = web.application(urls, globals())

render = web.template.render('templates/')

class Index(object):
    def GET(self):
        return render.hello_form1()

    def POST(self):
        form = web.input(name="Nobody", greet="Hello")
        greeting = "%s, %s" % (form.greet, form.name)
        return render.index(greeting = greeting)
if __name__ == "__main__":
    app.run()

Where should I add this block of text?

import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='mylog')
channel.basic_publish(exchange='', routing_key='mylog', body=$greeting)

Upvotes: 0

Views: 67

Answers (1)

gus27
gus27

Reputation: 2656

I'd suggest to put it in def POST(self):

class Index(object):
...
    def POST(self):
        form = web.input(name="Nobody", greet="Hello")
        greeting = "%s, %s" % (form.greet, form.name)

        connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
        channel = connection.channel()
        channel.queue_declare(queue='mylog')
        channel.basic_publish(exchange='', routing_key='mylog', body=greeting)

        return render.index(greeting = greeting)

Upvotes: 1

Related Questions