Chris Hawkes
Chris Hawkes

Reputation: 12420

Delete List Item From Database

This is probably a simple question, I have a form that you put in a comment it redirects to another page upon submit and displays all comments in a list. I am wondering how i can add an erase button to each list item to remove that particular comment.

Thank you in advance,

db.define_table('discussion',
                Field('comment', 'text'))

def comment():
    form = SQLFORM(db.discussion, _class='test1')
    if form.process().accepted:
        redirect(URL('comment_results'))
    return dict(form=form)  

def comment_results():
    items = db(db.discussion.id==db.discussion.id).select()
    ???? erase = db(db.discussion.id==).delete() ????
    ### trying to create an erase button to delete the currently displayed comment ###
    return dict(items=items, erase=erase)

view:
<html>
    <head>
        <link rel="stylesheet" href="{{=URL('static','css/test.css')}}">
    </head>
        <body>
            {{for item in items:}}
            <li>{{=item.id}} Comment = {{=item.comment}}<button id="{{=erase}}">erase</button></li> 
            {{pass}}
        </body>
</html>

* Answer * View:

<html>
    <head>
        <link rel="stylesheet" href="{{=URL('static','css/test.css')}}">
    </head>
        <body>
            {{for item in items:}}
            {{=item.comment}}<a href="{{=URL('delete', args=item.id)}}">  Delete</a>
            {{pass}}
        </body>
</html>

* I just passed the args to the delete method in the controller * Controller:

def delete():
    query = db(db.discussion.id==request.args(0)).select().first()  ## grabbing comment to be deleted from comment_results
    remove = db(db.discussion.id==query).delete()
    if remove:
        redirect(URL('comment_results'))
    return dict(remove=remove)

Upvotes: 1

Views: 1861

Answers (2)

craniumonempty
craniumonempty

Reputation: 3535

if you look here http://web2py.com/book/default/chapter/07#SQLFORM-and-insert/update/delete

They mention a field called deletable for appadmin that might be it.

Maybe try:

    form = SQLFORM(db.discussion, _class='test1', deletable=True)

for that one line to see if it works.

edit: sorry, wrong field. You'll have to create a form though.

I think that's it, because it says on the page I linked:

An update form is very similar to a create form except that it is pre-populated with the current record and it previews images. By default deletable = True which means the update form will display a "delete record" option.

Upvotes: 1

mayotic
mayotic

Reputation: 340

if you are using Django you would have to generate, through template code i would imagine, a view controller for each comment box. I am unaware how you have ur database structured but each comment might have a linking id to the original article and an unique id of its own in the table you would store the comments. using MVC u would generate those comment boxes and render them while retaining the unique comment ID and the subroutine operations the comment controller needs to have; then when u hit delete, the controller containing the unique id of the comment would query the database and voiala through ur MVC comment template... at least that's how i would approach such a comment system when utilizing MVC paradigm coding

i hope u would have some kind of a check in place to prevent database injections attacks and user privileges as well...

Upvotes: 0

Related Questions