#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    11
    Rep Power
    0

    filling in a form


    Is it possible to repopulate a form using Pyton?

    After the user has pressed the SUBMIT button, I save the forms data into a dictionary and then validate the input. If errors are found or no data was entered, the apprpriate error message(s) are displayed and a blank form is shown under the error messages. I can't figure out how to set the 'value' property to the previously entered value.

    For example in PHP I would write
    PHP Code:
    <b>user name:</b><input type=\"text\" name=\"user_name\" value=\"<? echo stored_user;?>\"></br>
    where "stored_user" is either blank (no value entered or theres an error with the value entered) or the previous entered data.

    Thanks for any help.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Yeah its very possible, and pretty easy; heres you're PHP code. However since this is CGI you cant embed Python in HTML but hey...

    Code:
    print '<b>user name:</b><input type="text" name="user_name" value="%s"></br>' % 'stored_user'
    You can use the cgi module to get form data, then just output the form with the appropriate data.

    http://www.python.org/doc/2.3.3/lib/module-cgi.html

    Edit: added a print statment to the beginning of the code block to make it more understandable...

    Mark.
    Last edited by netytan; February 24th, 2004 at 07:14 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    11
    Rep Power
    0
    I'm writing the script in Python, not PHP. I was just using PHP to show what I was trying to do as I'm just learning Python.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'm fully aware of why you posted the PHP code dude . And if you give it a go you'll find that this is exactly what you wanted... a pythonic version of you're PHP code

    Good luck with the project,

    Mark.
    Last edited by netytan; February 24th, 2004 at 07:13 PM.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Norway
    Posts
    41
    Rep Power
    12
    I just discovered FormEncode, which is supposed to do what you need and then some. http://formencode.org/
    Maybe a little complicated for simple form though.

    I am also working on my own form module to take care of form input and output. I have a form base class that implements common functionality for forms and classes for describing the different form controls defined by html.
    One example is the Textarea class which implements a textarea :
    Code:
    class Textarea(SingleItem):
      def __init__(self, name, rows, cols, value=""):
        SingleItem.__init__(self, name, value)
        self.rows = rows
        self.cols = cols
    
      def output(self):
        return """<textarea name="%s" rows="%s" cols="%s">
        %s
        </textarea>"""%(self.name, self.rows, self.cols, self.value)
    The SingleItem class ((base class for form controls with only one option) and it's parent, FormItem(base class for all form controls)) takes care of setting the name and the value of the form control. It might look like a lot of work for a single form, but I am doing this to have some code I can just plug in to any Python web application.
    Good web hosting info - articles about web hosting
    hb's web dev blog

IMN logo majestic logo threadwatch logo seochat tools logo