如何确定按下了哪个按钮

时间:2019-08-08 05:42:59

标签: html django python-3.x

我有一个表,其中填充了数据库中的数据。其中一些我具有删除按钮的额外功能。但我不明白如何在Django中按下删除按钮

def profile_view(request):

    if 'data_delete_id' in request.POST:
    # i don't know how to determine the id in the button   
'slug'

2 个答案:

答案 0 :(得分:1)

这可能很简单。您可以先获取按钮的名称,然后解析ID,例如:

def profile_view(request):

    delete_button_id = ""
    for name in request.POST.values():
        if name.startswith('data_delete_'):
            delete_button_id = int(name.split('_')[2])
            break

    # you now have the id in your delete_button_id variable

答案 1 :(得分:0)

根据Django Docs for HttpRequest.POST,您的python代码必须能够按预期工作,因为POST返回了类似Object的字典。

def profile_view(request):

    if 'data_delete_id' in request.POST:

但是在模板中,您正在使用<input type="button"。 这只会触发客户端操作。如果您没有此处未显示的任何JS代码,那么我想您需要使用<input type="submit"

我还建议使用<button type="submit"代替input,因为从语义的角度来看,它更准确。