OK just replace

recipe=recipes[0]

with

recipe=recipes[0] if recipes else None

On Oct 10, 5:57 am, billf <[EMAIL PROTECTED]> wrote:
> I have gone down the route of using the following controller code
> (I've left out a few checks for simplicity)
>
> recipes=db(db.recipe.id==id).select()
> recipe=recipes[0]
> form=SQLFORM(db.recipe, recipe)
> return dict(form=form)
>
> ... and in the view
>
> <form name="{{=form.formname}}" method="post"
> action="controller_name">
> <input type="hidden" name="id" value="{{=form.record.id}}">
>
> <label>Name:</label><input type="text" name="{{=form.formname}}_name"
> size="50" maxlength="50" value="{{=form.record.name}}"/>
> etc.
>
> This seems to work well BUT I want to use the same view for insert and
> update and when inserting there is no record!  It would be great to be
> able to call a function that creates an recipe record with id=0 and
> all fields set to default values.  It must be quite simple by
> iterating through the columns of the table definition but I don't
> really know enough yet to just code it.  More importantly does the
> function already exist?  I don't want to rely on cloning an existing
> instance.
>
> Bill
>
> On Oct 10, 5:08 am, mdipierro <[EMAIL PROTECTED]> wrote:
>
> > form is the form, form[0] is the table inside it. form[0][0] is the
> > first row. form[0][-1] is the last row, etc.
> > You can do
>
> >     form[0].append(TR('Label',TAG.button('whatever'),'comment'))
>
> > You can add more attributes to your TAG.button
>
> > On Oct 9, 8:39 pm, Jose de Oliveira Filho <[EMAIL PROTECTED]>
> > wrote:
>
> > > Thanks a bunch, Massimo. I completely overlooked the "hidden" thing
> > > in the FORM source, but the first answer is what I was looking for.
>
> > > I need to add an extra button to a SQLFORM, like "Save and Add
> > > another". I could put it outside the form but it looks really bad,
> > > any recommendations here ?
>
> > > By the way, did you ever think of turning the SQLFORM generation into
> > > divs instead of a table ?
>
> > > Thanks again,
>
> > > Deodoro Filho
>
> > > Em 09/10/2008, às 18:04, mdipierro escreveu:
>
> > > > Good questions. Answers below.
>
> > > >> (in controller:)
> > > >> def new_project():
> > > >>    f = FORM(INPUT(_name = "project_title", _type = "text"))
> > > >>    if f.accepts(request.vars, session):
> > > >>       db.project.insert(dict(title = f.vars.title, description =
> > > >> f.vars.description, user = session.user_id))
> > > >>       redirect(URL(r = request, f = "list"))
> > > >>    else:
> > > >>       return dict(form = f)
>
> > > >> My question is: is there some way of doing:
> > > >> ...db.insert.project(f.vars)...
> > > >> ?
>
> > > >> I'd be awesome if I could make "user" a hidden field directly.
> > > >> That'd be like:
>
> > > >> ...f = SQLFORM(db.project, hidden = [db.project.user])
> > > >>    f.vars.user = session.user_id...
>
> > > >> and then remove the db.project.insert line.
>
> > > > There are many ways you can do it:
>
> > > > 1) the recommended way
>
> > > >      # list only the fields you want and pass the others directly to
> > > > the vars
> > > >      f=SQLFORM(db.project,fields=['title'])
> > > >      f.vars.user=session.user_id
>
> > > > 2) use a hidden field
>
> > > > f=SQLFORM(db.project,fields=['title'],hidden=dict
> > > > (user=session.user_id))
> > > >      #but now the visitor can tamper with the hidden field in the form
>
> > > > 3) Manually using
>
> > > >     ## vars has to be a dictionary (like form.vars) and must only
> > > > contain valid fields, not including id.
> > > >     vars=form.vars
> > > >     vars.user=session.user_id
> > > >     db.project.insert(**vars)
> > > >     ## the ** unpacks the dictionary into named arguments
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to