Send HTML form submission to python script

4,355 views
Skip to first unread message

adohertyd

unread,
Jul 3, 2012, 3:15:12 PM7/3/12
to web...@googlegroups.com
I need to send some HTML form data to a python script for formatting but don't know how to do this. The user enters the data into a HTML form. The data is sent to a script where it is cleaned up and tokenized. The revised data is sent to another python function where it is to be used. How do I go about this? Does every python function go into the default.py controller or should I create a new .py file to process the form data? I have an example here. This takes the search term as input by the user and sends it to the results page. How would I send it to a python script before sending it to the results page? I'm really confused by this!

index.html:

<div id="MainArea">
 
<p align="center">MY SEARCH ENGINE</p>
 
<form name="form1" method="get" action="results.html">
   
<label for="SearchBar"></label>
   
<div align="center">
     
<input name="query" type="text" id="SearchBar" value="" size = "100px"><br />
     
<input name="submit" type="submit" value="Search">
   
</div>
 
</form>
 
<p align="center">&nbsp;</p>
 
</div>

results.html

{{extend 'layout.html'}}
<h1>This is the default/results.html template</h1>
{{=BEAUTIFY(response._vars)}}


<div>{{=results}}</
div>

default.py:

import urllib2


def index():
   
return dict()
 
def results():
    address
= "http://www.blekko.com/?q=%(query)s+/json&auth=<mykey>" % dict(query=request.vars.query)
    response
= urllib2.urlopen(address)
    html
=response.read()
   
return html



Massimo Di Pierro

unread,
Jul 3, 2012, 6:32:10 PM7/3/12
to web...@googlegroups.com

index.html:

<div id="MainArea">
 
<p align="center">MY SEARCH ENGINE</p>

 
<form name="form1" method="get" action="{{=URL('results')}}">

   
<label for="SearchBar"></label>
   
<div align="center">
     
<input name="query" type="text" id="SearchBar" value="" size = "100px"><br />
     
<input name="submit" type="submit" value="Search">
   
</div>
 
</form>
 
<p align="center">&nbsp;</p>
 
</div>
results.html

{{extend 'layout.html'}}
<h1>This is the default/results.html template</h1>
{{=BEAUTIFY(response._vars)}}


<div>{{=results}}</
div>

default.py:

import urllib


def index():
   
return dict()
 
def results():
    address
= "http://www.blekko.com/?q=%(query)s+/json&auth=<mykey>" % dict(query=request.vars.query)

    results 
= urllib.urlopen(address).read()
    return dict(results=results)



adohertyd

unread,
Jul 4, 2012, 6:14:18 AM7/4/12
to web...@googlegroups.com
Hi Massimo,

Thanks for that but it doesn't really answer my question. When I return dict(results=results), I want to pass that to a script, lets say 'process.py' to tokenize it etc. My question is: Do I create a new controller called process.py (which won't have a corresponding HTML page), or do I create a model? My app doesn't have a database, it doesn't need one. I am dealing with one user input term that returns 3 sets of json data which will be parsed and output directly. It doesn't need to be stored. Can you give me any info regarding that. I'm just not sure where I should be creating my .py files to do the processing.

Anthony

unread,
Jul 4, 2012, 9:10:02 AM7/4/12
to web...@googlegroups.com
Thanks for that but it doesn't really answer my question. When I return dict(results=results), I want to pass that to a script, lets say 'process.py' to tokenize it etc. My question is: Do I create a new controller called process.py (which won't have a corresponding HTML page), or do I create a model? My app doesn't have a database, it doesn't need one. I am dealing with one user input term that returns 3 sets of json data which will be parsed and output directly. It doesn't need to be stored. Can you give me any info regarding that. I'm just not sure where I should be creating my .py files to do the processing.

If you need to call other functions from your controller action, you can put those other functions inside (a) the same controller (any function in a controller that starts with a double underscore or takes arguments will not be exposed as a URL), (b) a model file, or (c) a module (from which you would then import it -- see http://web2py.com/books/default/chapter/29/4#Accessing-the-API-from-Python-modules).

Anthony 

adohertyd

unread,
Jul 4, 2012, 12:39:12 PM7/4/12
to web...@googlegroups.com
Thanks Anthony. This has been bothering me for ages because I couldn't decide where to put the python code. Much appreciated.
Reply all
Reply to author
Forward
0 new messages