How do I get an argument in Web2Py

1

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

I'm trying through this code

def off ():

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)
    
asked by anonymous 13.10.2016 / 23:41

1 answer

1

Use SQLFORM.factory to do this, follows an 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).

    
26.10.2016 / 06:14