从数据库中删除列表项
这可能是一个简单的问题,我有一个表单,您可以将其放入评论中,它在提交后重定向到另一个页面并在列表中显示所有评论。我想知道如何向每个列表项添加擦除按钮以删除该特定评论。
预先感谢您,
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>
* 回答 * 查看:
<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>
*我刚刚将参数传递给控制器中的删除方法* 控制器:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你看这里 http://web2py.com/book /default/chapter/07#SQLFORM-and-insert/update/delete
他们提到了一个名为 appadmin 的可删除字段,这可能就是它。
也许尝试一下:
对于这一行,看看它是否有效。
编辑:抱歉,错误的字段。不过,您必须创建一个表单。
我想就是这样,因为我链接的页面上写着:
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:
如果您使用 Django,则必须通过我想象的模板代码为每个评论框生成一个视图控制器。我不知道你的数据库是如何构建的,但每个评论可能都有一个到原始文章的链接 id,并且在你存储评论的表中拥有自己的唯一 id。使用 MVC,您将生成这些评论框并呈现它们,同时保留唯一的评论 ID 和评论控制器需要的子例程操作;然后,当你点击删除时,包含评论的唯一 id 的控制器将通过你的 MVC 评论模板查询数据库并瞧瞧...
至少这就是我在使用 MVC 范式编码时处理这样一个评论系统的方式,
我希望你能有某种检查来防止数据库注入攻击和用户权限......
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...