从数据库中删除列表项

发布于 2024-12-17 04:40:04 字数 1764 浏览 3 评论 0原文

这可能是一个简单的问题,我有一个表单,您可以将其放入评论中,它在提交后重定向到另一个页面并在列表中显示所有评论。我想知道如何向每个列表项添加擦除按钮以删除该特定评论。

预先感谢您,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦纸 2024-12-24 04:40:04

如果你看这里 http://web2py.com/book /default/chapter/07#SQLFORM-and-insert/update/delete

他们提到了一个名为 appadmin 的可删除字段,这可能就是它。

也许尝试一下:

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

对于这一行,看看它是否有效。

编辑:抱歉,错误的字段。不过,您必须创建一个表单。

我想就是这样,因为我链接的页面上写着:

更新表单与创建表单非常相似,不同之处在于它是
预先填充当前记录并预览图像。经过
默认可删除 = True 这意味着更新表单将显示
“删除记录”选项。

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.

眼泪也成诗 2024-12-24 04:40:04

如果您使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文