You should not call form.process twice. Does idToEdit come from a URL arg
or var? Maybe something like this:
def myform():
record = db.langResource(request.args(0))
form = SQLFORM(db.langResource, record=record).process(
message_onsuccess='Resource modified' if record else 'New resource
added')
return dict(form=form)
If request.args(0) is empty, db.langResource(request.args(0)) will just
return None. You can set the flash message in .process() conditionally.
There is already a built-in flash message when there are errors, so no need
to add that. Also, do not set session=None unless you want to leave the
form open to CSRF attacks.
Anthony
On Thursday, April 17, 2014 4:11:10 PM UTC-4, Sharon Correll wrote:
> I am a web2py newbie, and I'm sure this is a very basic question, but I
> haven't found a post that addresses it exactly.
>
> I am trying to create a form that modifies an existing record. My code,
> which is modeled after stuff I saw in the tutorial, looks like:
>
> if idToEdit :
> record = db.langResource(idToEdit)
> sqlForm = SQLFORM(db.langResource, record) # update existing
> else :
> sqlForm = SQLFORM(db.langResource) # create new
>
> if sqlForm.process(session=None,
> formname="langResource/create").accepted:
> response.flash = 'New resource added.'
> elif sqlForm.process(session=None,
> formname="langResource/modify").accepted:
> response.flash = 'Resource modified.'
> elif sqlForm.errors:
> response.flash = 'Form has errors.'
> else:
> response.flash = 'Please fill out the form.'
> return sqlForm
>
> When idToEdit is set to something, the resulting form is nicely populated
> with the values of the existing record. However, when I press Submit, a new
> record is added with the changes, and with a new unique ID. The old record
> is still there.
>
> I had originally tried to create the HTML by hand, so I could customize
> it, and had the same problem, so I thought my HTML was missing something.
> But using the SQLFORM directly does the same thing.
>
> I did see this bit in the HTML: <input name="id" type="hidden" value="5"
> /> - and wondered if that is the magic bit that makes it know that it is
> supposed to update an existing record. But if so it doesn't seem to be
> working.
>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.