How do I get an argument in Web2py

Asked

Viewed 71 times

2

I am with the following problem, I am producing an application in W2P, with a CRUD, on the part of deleting an item I am wanting to get the id through a form and that id remove the item the DB.

I’m trying through this code

def erase():

form = FORM('Informe o ID do item que deseja remover', INPUT(_name='id'),   INPUT(_type='submit'))

db(lista.id==request.args(0, cast=int)).delete()

return dict(form=form)

1 answer

1

Use the SQLFORM.Factory to do this, follows example:

def apagar():
    form = SQLFORM.factory(Field('id_deletar', label='Informe id...'))
    if form.process().accepted:
        id_deletar = form.vars.id_deletar
        db(db.lista.id==id_deletar).delete()
    return dict(form=form)

Another option, using URI arguments, would be:

def apagar():
    id_deletar = request.args(0)
    if id_deletar:
        db(db.lista.id==id_deletar).delete()
        form = False
    else:
        form = SQLFORM.factory(Field('id_deletar', label='Informe id...'))
        if form.process().accepted:
            id_deletar = form.vars.id_deletar
            redirect('apagar', args=id_deletar)
    return dict(form=form)

However, the first option is really the most ideal. Since it is a good practice not to make changes to GET requests (second example), but through POST (first example).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.