I have extended *pyramid_deform.FormView* for my add/edit views in my app.
I can add records to the db alright but on the edit page, the form doesn't
get populated by the data in the *appstruct* . The code for the add/edit
views together with my view registration code is shown below:
# ======== import statements, file: views.py =============
>
>
>
> def get_appstruct(context, schema):
>
> appstruct = {}
>
> for field in schema.children:
>
> if hasattr(context, field.name):
>
> appstruct[field.name] = getattr(context, field.name)
>
> return appstruct
>
> class UserEditPage(FormView, Dashboard):
>
> schema = UserSchema()
>
> buttons = ('save',)
>
> form_options = (('formid', 'new-user'), ('method', 'POST'),
>> ('bootstrap_form_style', 'form-vertical'))
>
> title = u'Edit User'
>
>
>
> def appstruct(self):
>
> return get_appstruct(self.request.context, self.schema)
>
>
>
> def save_success(self, appstruct):
>
> ctx = self.request.context
>
> ctx.username = appstruct['username']
>
> ctx.firstname = appstruct['firstname']
>
> ctx.lastname = appstruct['lastname']
>
> ctx.dob = appstruct['dob']
>
> self.request.session.flash(u'Your changes were saved')
>>
> return HTTPFound(location=self.request.route_url('view_users'))
>
>
>>
>> class UserAddPage(FormView, Dashboard):
>
> buttons = ('save',)
>
> schema = UserSchema()
>
> title = u'Add New User'
>
>
>
> def save_success(self, appstruct):
>
> new_user = User(**appstruct)
>>
> db.add(new_user)
>
> self.request.session.flash(u'User was successfully added', 'success')
>
> return HTTPFound(location=self.request.route_url(view_users))
>
> # ================ add views, file: __init__.py =====================
>
> config.add_view(UserAddPage, route_name='new_user',
>> renderer='templates/users/edit.pt')
>
> config.add_view(UserEditPage, route_name='edit_user',
>> renderer='templates/users/edit.pt')
>
>
>
>
*- *I am completely lost when it comes to the workings of
the overridden method* *UserEditPage::appstruct(). Although, I do
understand that it's a dict of data I wish to prefill the form with.
*- *Am I supposed to make a call to the db to retrieve the data to populate
appstruct?
*Would anyone be kind enough to help me figure out what I am doing wrong
here? Thanks so much.*
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.