How subclassing the Form class ?

14 views
Skip to first unread message

ProfessionalIT

unread,
Jul 14, 2010, 10:48:11 AM7/14/10
to web.py
Hi Friends,

Sorry for the dummie question but....

To use the forms in GAE - Google App Engine I did a copy of form.py
called gae_form.py and re-defined the function attrget (because the
original function don't accept the key attribute of GAE Entitys).

Then in my forms.py (my forms file definition) I'm using:

####from web import form
import gae_form as form

and

def getCategoryForm():
category_form = form.Form(
form.Textbox('title'),
form.Button('Submit!')
)
return category_form

Well, my main question is: How to subclassing the web.form class(if
this is necessary ?) ? because I'm only redefining the attrget
function of the original form.py file. I think that my solution(a copy
of original form.py) don't be the best way.

Any idea, suggestion ?

Thanks for all help.

best regards,
Leandro.

ProfessionalIT

unread,
Jul 15, 2010, 9:33:55 AM7/15/10
to web.py
Friends,
Any idea or suggestion ?
Thanks for all help.

-- Leandro.

Josi Sibande

unread,
Jul 16, 2010, 2:44:43 AM7/16/10
to web.py
WTForms can save your life.

Alex K

unread,
Jul 16, 2010, 6:31:27 AM7/16/10
to web.py
Hi Josi,

WTForms seem really cool. How does it compare with webpy forms? It
seems very much inspired by webpy forms. From my experience I found
webpy forms to be hard to debug.

How does WTForms compare with other heavier form packages?

Thank you,

Alex

Josi Sibande

unread,
Jul 18, 2010, 4:06:45 AM7/18/10
to web.py
Alex

Web.py forms is good and also very minimal (which is what led me to
look at alternatives). WTForms is similar to Django forms, but more
powerful and beautifully designed according to me. Give it a try,
you'll love it.

bob

unread,
Jul 15, 2010, 4:34:46 PM7/15/10
to web.py
See http://www.mail-archive.com/we...@googlegroups.com/msg05863.html

Basically...

class MyForm(web.form.Form):
def attrget(self):
# override parent's attrget method

some_form = MyForm(
....
)

ProfessionalIT

unread,
Jul 19, 2010, 8:33:45 AM7/19/10
to web.py
Hi Bob,

The problem is: the def attrget() function is out of original Form
class (see in form.py file in the web folder in the web.py
instalation), it's a function in the form.py file but out of the Form
class definition.

Another problem is when I'm try subclassing the Form class I'm
receiving this error message:

vemail = form.regexp("^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]
+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$", "Precisa ser um endereco
de e-mail válido !.")
AttributeError: 'module' object has no attribute 'regexp'

Or be, another class in the form.py file aren't sub classified or
aren't in the same namespace.

Any idea ?

-- Leandro.

On Jul 15, 5:34 pm, bob <rob.kr...@gmail.com> wrote:
> Seehttp://www.mail-archive.com/we...@googlegroups.com/msg05863.html

ProfessionalIT

unread,
Jul 19, 2010, 9:13:15 AM7/19/10
to web.py
Friends,

I don't know if my solution is the best way to solve, but...follow
my GAEForm implementation class:

###### gae_form.py

from web.form import *
import logging
from google.appengine.ext import db

def attrget(obj, attr, value=None):
if isinstance(obj,db.Model):
try:
if attr == 'key':
return obj.key()
gae_attribute = getattr(obj,attr)
if isinstance(gae_attribute,db.Model):
id_reference = gae_attribute.key().id()
return id_reference
else:
return gae_attribute
except Exception, ex:
logging.debug(ex)
else:
if hasattr(obj, 'has_key') and obj.has_key(attr): return
obj[attr]
if hasattr(obj, attr): return getattr(obj, attr)
return value

class GAEForm(Form):
pass

###### forms.py My forms declaration form file.

import gae_form as form

import data

def getPostForm():

category_list = data.all_categories()

category_options = [(row.key().id(), row.title) for row in
category_list]

post_form = form.Form(

form.Hidden('key'),

form.Dropdown('category', category_options),

form.Textbox('title'),

form.Textarea('body'),

form.Dropdown('language', [('pt', 'Portuguese'), ('en',
'English')]),

form.Button('Submit!')

)

return post_form

Now is running ok. If someone has a better suggestion, please, I'm
open to opinions and other solutions.

Really, the web.py is simple, elegant and a efficient solution for web
development in python.
Reply all
Reply to author
Forward
0 new messages