删除记录而不直接访问它

时间:2018-01-30 11:08:45

标签: django django-models

我正在django中创建一个数据可视化站点,并使用其余的api来获取我的数据。有没有办法在不直接访问其URL的情况下删除记录,因为在这种情况下它是不可能的。

这样的东西
def employee_delete(request):
    instance = Employee.objects.get(social_security=request.POST)
    instance.delete()
    return render(request, "dashboard.html")

只有在我学会了你可以访问控制台的情况下才有效,所以我试图从这样的表格中访问数据

def employee_delete(request):
    if request.method == "POST":
        form = delete_EmployeeForm(request.POST, request.FILES)
        if form.is_valid():
            instance = Employee.objects.get(social_security=request.POST)
            instance.delete()
            return render(request, "dashboard.html")
    else:
        form = delete_EmployeeForm()
    return render(request, "deleteemployee.html",{'form': form})

如果我能够更具体地说明我从表单中访问了哪些数据,这是否可行?我得到一个typeError试图以这种方式使用request.Post。该表单包含Employee模型中“social_security”中的单个字段。

由于

1 个答案:

答案 0 :(得分:1)

def employee_delete(request):
    if request.method == "POST":
        form = delete_EmployeeForm(request.POST, request.FILES)
        if form.is_valid():
            instance = Employee.objects.get(social_security=request.POST['social_security'])
            instance.delete()
            return render(request, "dashboard.html")
    else:
        form = delete_EmployeeForm()
    return render(request, "deleteemployee.html",{'form': form})

在您的视图中使用此