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)
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:
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: