Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8189561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:19:16+00:00 2026-06-07T03:19:16+00:00

I need to send some HTML form data to a python script for formatting

  • 0

I need to send some HTML form data to a python script for formatting in a web2py application 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:19:17+00:00Added an answer on June 7, 2026 at 3:19 am

    It doesn’t look like you are using the built-in form mechanisms. I am curious — how exactly do you need to clean up your data?

    Normally, you would create a controller file with a function that builds the form and also has a branch to process it. In your data model, you would specify what is acceptable input, and in the form processing function (controller function) you would manipulate the data.

    You can also use

    form.process(..., onvalidation=some_func)
    

    Where onvalidation is a function that can further process your form, create/manipulate values before they are stored in the DB (or you can not store them in DB at all), and do further validations.

    See: http://web2py.com/books/default/chapter/29/7#onvalidation

    Web2py forms generally self-submit and have a session key set to prevent double submissions and the like. The way you’re doing it you would have to make sure to use form.accepts(…) without session or skip the web2py form.accepts/form.process and just use request.vars.field_name_that_you_provided to get the form field data. Then you would have to validate the data yourself.

    I would recommend creating a web2py model (models/form.py), or at the very least creating the form using web2py’s FORM helper inside the controller file that is responsible for displaying and processing your form.

    This is the web2py way — a single function that either returns a form to fill in or processes it. You can use redirection to send them to another page if everything is groovy.

    def display_form():
        form = FORM('Your name:',
                INPUT(_name='name', requires=IS_NOT_EMPTY()),
                INPUT(_type='submit'))
        if form.process().accepted:
            session.flash = 'form accepted'
            redirect(URL('next'))
        elif form.errors:
            response.flash = 'form has errors'
        else:
            response.flash = 'please fill the form'
        return dict(form=form)
    

    another way is this:

    def process_form():
       validated_data = dict()
       for k,v in request.vars.values(): # I think this works...
          if k == 'some_key':
             do_some_processing()
          ...
          validated_data[k] = some_func(v)
          ...
       # now everyhing is validated and ok, send the data on
       redirect('next_page', vars=validated_data)
    

    The redirect will send the validated form data onto the next function. If you don’t need to send it to a web2py function, then execute whatever function is required from the process_form function.

    Make sense?

    EDIT: Another way of saying this is that the script contents should execute in the form processing function. You can define a controller function that is explicitly executed by another function in the same controller file. It would not have a view associated and would not be a standalone page, but just a function.

    You can also use execfile(“path/to/file”). I would recommend putting the script in applications//private/ and using request.folder to get the folder the app lives in, then just tack on private…so something like:

    execfile(os.path.join(request.folder, 'private', 'script.py'))
    

    But you’ll have to verify that gets you the proper path.

    EDIT (Validating Forms): Check out the web2py book on how to make data models and use forms/validators ( http://web2py.com/books/default/chapter/29/7 ). When you define a web2py data model, you can include validators directly on the field definition. This way, the database will know if it’s an integer or string, and you can force lengths or regex patterns on the input. The advantage is that the form you create with:

    form = SQLFORM(db.some_table)
    

    or even:
    form = SQLFORM.factory(Field(‘some_field’, ‘integer’), Field(‘some_str’, ‘string’, requires=IS_ALPHANUMERIC))

    The validation is very easy — the integer typed fields will immediately delete any non-numeric values entered, and even if you modified the source code or sent a POST/GET request with bad values, the form.process(…) method would invalidate it and allow you to easily highlight the errors.

    Here’s an example form definition:

    db.define_table('news_item',
      Field('title', 'string', label='Title', requires=IS_NOT_EMPTY()),
      Field('type', 'string', label='Type', requires=IS_IN_SET(news_types)),
      Field('pub_date', 'date', default=request.now),
      Field('post_date', 'date', default=request.now), # Set date defaults 
      Field('link', 'string', label='News Link', requires=IS_EMPTY_OR(IS_URL())),
      Field('img', 'upload', uploadfield='img_file', label='Story Title Image'),
      Field('img_file', 'blob', label='Story Title Image'),
      Field('tagline', 'text', label='Tagline', requires=IS_NOT_EMPTY()),
      Field('published', 'boolean', label='Published?', notnull=True, required=True),
    )
    

    Which will create a table called news_item in a database defined earlier in the program (db = DAL(“…connection string”) ), and allow you to create a good-looking form instantly. Notice there are many option paramaters.

    You can also skip the DB and make forms directly, but I’ll let you look that up in the docs 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send some data from my form in html to webservice. (For
I need to send some data with radio button. This buttons are <td> <span
I need to implement some form of communication mechanism in my application, to send
In my application I need to send and receive HTML in string form. I'd
I need to send some byte array from android device to Servlet. For this
I need to be able to send an image and some form fields from
i need to send raw multipart data with a php POST but without an
I have a common HTML non-xpages form(part of non-xpages application) and I need to
I need to send some bytes over UDP Protocol the start squence is 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
I need to send some information on a VxWorks message queue. The information to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.