Redirect after deletion for crud.update form

80 views
Skip to first unread message

davosmith

unread,
Aug 11, 2010, 4:18:29 AM8/11/10
to web2py-users
I have a series of linked database tables:

'course' which contains several 'units' which contain several
'topics'.

On the 'unit' page, I display the details of the unit, a list of
topics and a link to edit the unit details.

When you click on the edit link, a crud.update form is created, which
sends the user back to the 'unit' page, once the update is complete.

The problem is, that if the user deletes the record for the unit, then
they need to be redirected to the 'course' page, rather than the
'unit' page (as this unit no longer exists to be displayed).

After looking through the source code, I can't find any way of doing
this - 'crud.settings.delete_next' is ignored in the crud.update form
and trying to alter the value of 'next' inside the 'ondelete'
function, will not work as the value of 'next' has already been copied
into a local variable by that point in the crud.update function.

Does anyone have any helpful suggestions?

mdipierro

unread,
Aug 11, 2010, 9:13:28 AM8/11/10
to web2py-users
Can you post an example? This

crud.settings.delete_next

should work.

Massimo

davosmith

unread,
Aug 11, 2010, 10:43:37 AM8/11/10
to web2py-users
def unitedit():
"""
edit a unit details
"""
unit = db.unit(request.args(0)) or redirect(URL('index'))
crud.settings.delete_next = URL('course', args=unit.course)
form = crud.update(db.unit, unit.id, next=URL('unit',
args=unit.id))
return dict(form=form)

If the user ticks the 'delete' box then submits the form, then they
are redirected to the 'unit' page, which drops them back to the index
page.
The code for the function 'crud.update' does not look at the variable
'crud.settings.delete_next' at all, as far as I can see.

I have found a solution, of sorts, which is to use the 'ondelete'
function to set a session variable with the 'courseid', which is then
checked by the 'unit' page which does an extra redirect to the course
page (after deleting the session variable).

Davo

mdipierro

unread,
Aug 11, 2010, 11:26:23 AM8/11/10
to web2py-users
The problem is here:

crud.settings.delete_next = URL('course', args=unit.course)
form = crud.update(db.unit, unit.id,
next=URL('unit',args=unit.id))

the latter next overrides the former.

You can do:

ondelete_next = URL('course', args=unit.course)
form = crud.update(db.unit, unit.id,
next=URL('unit',args=unit.id),
ondelete=(lambda form, next=ondelete_next:
redirect(next)))
Reply all
Reply to author
Forward
0 new messages