Error using wtforms with jinja model_form()

1,325 views
Skip to first unread message

Jakob Holmelund

unread,
Jan 3, 2011, 3:12:14 PM1/3/11
to tipfy
Hi all i'm trying to use model_form from wtforms on my app engine
model. The model looks like this

class Test(db.Model):
name = db.StringProperty(required=True)
adress = db.StringProperty(required=True)

and i'm initializing the model_form like this.

---
handelsers.py

from wtforms.ext.appengine.db import model_form
from tipfy.ext.jinja2 import render_response

form = model_form(Restaurant,only=['name'])

context = {
"form":form
}
return render_response('index2.html',**context)

---

and the jinja-template like this

---

{% from '_jinja_form_macros.html' import form_field %} <-- Using the
jinja macro's from tipfy's homepage

<form id="create-form" method="post" action="/create_restaurant">
{{ form_field(form.name, class='medium') }}
{{ form_field(form.adress, class='medium') }}
<input id="submit" type="submit" name="submit" disabled="true" /
>
</form>

i've tried everything i could possibly think of, and my thought right
now is pointing to the jinja macro'sm the error can be seen below..
Sadly google tell's me that no one have ever encountered this problem,
so please help ! I've tried logging the field form.name through
pythons logging and it tells me

---

INFO 2011-01-03 20:08:46,625 handlers.py:24]
<UnboundField(TextField, (), {'
default': None, 'validators': [<wtforms.wtforms.validators.Required
object at 0x
0889BD70>, <wtforms.wtforms.validators.Length object at 0x0888E1D0>],
'label': '
Name'})>
---

So there is definitely a field called label ! :/ Error below

Traceback (most recent call last):

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\distlib\tipfy
\__init__.py", line 442, in wsgi_app
response = self.handle_exception(request, e)

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\distlib\tipfy
\__init__.py", line 430, in wsgi_app
rv = self.dispatch(request)

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\distlib\tipfy
\__init__.py", line 559, in dispatch
return handler(self, request)(method, **request.rule_args)

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\distlib\tipfy
\__init__.py", line 152, in __call__
return method(*args, **kwargs)

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\apps\restaurants
\handlers.py", line 30, in get
return render_response('index2.html',**context)

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\distlib\tipfy\ext
\jinja2\__init__.py", line 172, in render_response
return Tipfy.app.response_class(render_template(filename,
**context),

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\distlib\tipfy\ext
\jinja2\__init__.py", line 159, in render_template
return jinja2.get_template(filename).render(**context)

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\distlib
\jinja2\environment.py", line 891, in render
return self.environment.handle_exception(exc_info, True)

File "C:\Users\jakobsuper\Documents\tipfy\anymenu\templates
\index2.html", line 85, in top-level template code
{{ form_field(form.name, class='medium') }}

UndefinedError: 'wtforms.fields.UnboundField object' has no attribute
'label'

Ragan Webber

unread,
Jan 3, 2011, 4:22:26 PM1/3/11
to ti...@googlegroups.com
Hi Jakob,
Could it be that you're specifying to use only 'name' in the model_form, but referencing form.adress in the template?

-Ragan


--
You received this message because you are subscribed to the Google Groups "tipfy" group.
To post to this group, send email to ti...@googlegroups.com.
To unsubscribe from this group, send email to tipfy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/tipfy?hl=en.


Jakob Holmelund

unread,
Jan 3, 2011, 4:46:30 PM1/3/11
to tipfy
Sadly not.. should have said

form = model_form(Restaurant)

On Jan 3, 10:22 pm, Ragan Webber <rag...@gmail.com> wrote:
> Hi Jakob,
> Could it be that you're specifying to use only 'name' in the model_form, but
> referencing form.adress in the template?
>
> -Ragan
>
> On Mon, Jan 3, 2011 at 12:12 PM, Jakob Holmelund
> <jakobholmel...@gmail.com>wrote:
> > tipfy+un...@googlegroups.com <tipfy%2Bunsu...@googlegroups.com>.

Bob

unread,
Jan 4, 2011, 11:50:18 AM1/4/11
to tipfy
If I understood the demos/docs that I've been able to find (and
there's a real good chance I didn't), I don't think you can build your
form directly from a db model. It seemed to me that you had to
instantiate your form class separately from your db class. That seemed
odd and counterintuitive to me, but that's how I was able to get it to
work. I was working form this tutorial.

http://www.franciscosouza.com/2010/09/flying-with-tipfy-on-google-app-engine/

Jakob Holmelund

unread,
Jan 4, 2011, 3:19:32 PM1/4/11
to tipfy
Yes Bob.. That was my initial thought.. But then i found this.

http://wtforms.simplecodes.com/docs/0.6.1/ext.html

It explains how the model_form should work.. and the i found this

http://groups.google.com/group/tipfy/browse_thread/thread/39eed6625e870b2b?fwc=2

and this

http://code.google.com/p/tipfy/issues/detail?id=7

So apparently somebody got it working :/ Will try with a manual form
now and see if it is jinja that is the problem..

On Jan 4, 5:50 pm, Bob <bob.ral...@gmail.com> wrote:
> If I understood the demos/docs that I've been able to find (and
> there's a real good chance I didn't), I don't think you can build your
> form directly from a db model. It seemed to me that you had to
> instantiate your form class separately from your db class. That seemed
> odd and counterintuitive to me, but that's how I was able to get it to
> work. I was working form this tutorial.
>
> http://www.franciscosouza.com/2010/09/flying-with-tipfy-on-google-app...

Jakob Holmelund

unread,
Jan 4, 2011, 3:58:53 PM1/4/11
to tipfy
I fixed it !!

for some fucked up reason it should look like this

Preform = model_form(Model)
form = Preform()

context = {
"form":form
}
return render_response('index2.html',**context)

Maybe it makes sence to somebody.. But not me :/

Bob

unread,
Jan 5, 2011, 11:52:17 AM1/5/11
to tipfy
Yeah, we could really use some better docs that simply explain the
benefits of wtforms and show a *full* example in tipfy. I'm sure
wtforms is solving some problems and making things easier somehow. But
IMHO with the level of docs available now it just seems a lot easier
to build your forms and validation yourself.

Mauro Asprea

unread,
Jan 5, 2011, 12:12:33 PM1/5/11
to ti...@googlegroups.com
It would be great if you could post a simple example of this :D Some simple model/form/handler at least to be here till we move it to the wiki. It would be really helpful 

Thanks!

--
You received this message because you are subscribed to the Google Groups "tipfy" group.
To post to this group, send email to ti...@googlegroups.com.
To unsubscribe from this group, send email to tipfy+un...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/tipfy?hl=en.




--
Mauro Sebastián Asprea

E-Mail: mauro...@gmail.com
Mobile: +34 654297582
Skype: mauro.asprea

Algunos hombres ven las cosas como son y se preguntan porque. Otros sueñan cosas que nunca fueron y se preguntan por qué no?.
George Bernard Shaw

Simon Payne

unread,
Jan 5, 2011, 12:57:00 PM1/5/11
to ti...@googlegroups.com
I never liked the forms tied up to forms too closely because it can easily happen that you add some field and you don't realize that it is populated also to the form... which could have unfortunate results.
Of course you have to check this and there are ways to avoid it but the code needed to write your own forms is not too long anyway. :)

2011/1/5 Mauro Asprea <mauro...@gmail.com>

Jakob Holmelund

unread,
Jan 5, 2011, 4:38:43 PM1/5/11
to tipfy
Thats what you got the only param for.. I will post a full example
later tonight..

On Jan 5, 6:57 pm, Simon Payne <i...@simonpayne.cz> wrote:
> I never liked the forms tied up to forms too closely because it can easily
> happen that you add some field and you don't realize that it is populated
> also to the form... which could have unfortunate results.
> Of course you have to check this and there are ways to avoid it but the code
> needed to write your own forms is not too long anyway. :)
>
> 2011/1/5 Mauro Asprea <mauroasp...@gmail.com>
>
>
>
>
>
>
>
> > It would be great if you could post a simple example of this :D Some simple
> > model/form/handler at least to be here till we move it to the wiki. It would
> > be really helpful
>
> > Thanks!
>
> > On Wed, Jan 5, 2011 at 5:52 PM, Bob <bob.ral...@gmail.com> wrote:
>
> >> Yeah, we could really use some better docs that simply explain the
> >> benefits of wtforms and show a *full* example in tipfy. I'm sure
> >> wtforms is solving some problems and making things easier somehow. But
> >> IMHO with the level of docs available now it just seems a lot easier
> >> to build your forms and validation yourself.
>
> >> On Jan 4, 2:58 pm, Jakob Holmelund <jakobholmel...@gmail.com> wrote:
> >> > I fixed it !!
>
> >> > for some fucked up reason it should look like this
>
> >> > Preform = model_form(Model)
> >> > form = Preform()
>
> >> > context = {
> >> >             "form":form
> >> >         }
> >> >         return render_response('index2.html',**context)
>
> >> > Maybe it makes sence to somebody.. But not me :/
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "tipfy" group.
> >> To post to this group, send email to ti...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> tipfy+un...@googlegroups.com <tipfy%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/tipfy?hl=en.
>
> > --
> > Mauro Sebastián Asprea
>
> > E-Mail: mauroasp...@gmail.com
> > Mobile: +34 654297582
> > Skype: mauro.asprea
>
> > Algunos hombres ven las cosas como son y se preguntan porque. Otros sueñan
> > cosas que nunca fueron y se preguntan por qué no?.
> > George Bernard Shaw
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "tipfy" group.
> > To post to this group, send email to ti...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > tipfy+un...@googlegroups.com <tipfy%2Bunsu...@googlegroups.com>.

Jakob Holmelund

unread,
Jan 6, 2011, 4:43:33 AM1/6/11
to tipfy
Here is an example.. I'm not home so excuse me if there are minor
syntax errors since this is made from my head.. But it should give you
a good idea of how it works.. There should definitely be a tutorial
for this on the tipfy site.. This took me like 2 days to figure :/ but
now i'm happy because i can save alot of time in the future. My
template.html is over simplified since i use javascript for form
validation, a good idea would be to use the jinja macros from the
tipfy site if you want to use wtforms validation. bare in mind that
you need a version of tipfy with wtforms for this to work, this where
also a pain for me, since it rendered impossible to build your own
version of tipfy on windows 7 x64, i fixed this by running a linux
distro in virtual box.. write me if you want more info.. I can send
you my files if you want to have a look.. Over and out..

models.py

class Post(db.Model):
title = db.StringProperty()
body = db.TextProperty()
added_by = db.StringProperty()

handlers.py

from wtforms.ext.appengine.db import model_form
from tipfy.ext.jinja2 import render_response
from models import Post

class PostHandler(RequestHandler):
def get(self):
model_post_form = model_form(Post,only=['title','body'])
post_form = model_post_form()
return render_response('template.html',form=post_form)
def post(self):
model_post_form = model_form(Post,only=['title','body'])
post_form = model_post_form(self.request.form)
post = Post()
if post_form.validate():
post_form.populate_obj(post)
post.added_by = users.users.get_current_user().email
post.put()
return redirect('somewhere')
return render_response('template.html',form=post_form)

template.html

<form action="." method="post">
<label for="title">{{form.title.label}}</label>
{{ form.title }}
<label for="body">{{form.body.label}}</label>
{{ form.body }}
<input type="submit" value="submit" />
</form>
Reply all
Reply to author
Forward
0 new messages