我无法理解,为什么只有 delete_node 不起作用
i am unable to understand ,why only delete_node not working
以下是来自用户的三个请求,create_node、rename_node 工作正常但 delete_node 请求无效。
requests:
[31/Aug/2018 17:00:53] "GET /buildknowledge/?operation=create_node&id=1&text=New+node HTTP/1.1" 200 16071
[31/Aug/2018 17:01:10] "GET /buildknowledge/?operation=rename_node&id=63&text=pkkkk HTTP/1.1" 200 16071
[31/Aug/2018 17:02:33] "GET /buildknowledge/?operation=delete_node&id=63 HTTP/1.1" 200 16071
views.py:
def basetest(request):
cu_id = request.user.id
if 'operation' in request.GET and "id" in request.GET and "text" in request.GET:
operation = request.GET['operation']
pid=request.GET['id']
nodename=request.GET['text']
if "create_node" == operation:
p=Buildkb4.objects.create (parent=pid,created_by=cu_id,text=nodename)
p.save()
elif "delete_node" == operation:
Buildkb4.objects.filter(pk=pid).delete()
elif "rename_node" == operation:
Buildkb4.objects.filter(pk=pid).update(text=nodename)
return render(request,"registration/basetest.html")
Note: It is not a good idea to use GET requests to perform side-effects (like updating or removing records in a database). Therefore I would advice to change such requests to a POST, DELETE or PATCH. More information at the bottom of this post.
根据您显示的日志,删除调用在 querystring 中带有两个参数:operation
和 id
。但是第一个 if
语句还需要一个 text
参数:
def basetest(request):
cu_id = request.user.id
<b>if 'operation' in request.GET and "id" in request.GET and "text" in request.GET:</b>
operation = request.GET['operation']
pid=request.GET['id']
nodename=request.GET['text']
if "create_node" == operation:
p=Buildkb4.objects.create (parent=pid,created_by=cu_id,text=nodename)
p.save()
elif "delete_node" == operation:
Buildkb4.objects.filter(pk=pid).delete()
elif "rename_node" == operation:
Buildkb4.objects.filter(pk=pid).update(text=nodename)
return render(request,"registration/basetest.html")
所以这永远不会达到 elif "delete_node" == operation:
的情况。
您可以通过减少检查限制来解决此问题,例如:
def basetest(request):
cu_id = request.user.id
if 'operation' in request.GET and "id" in request.GET:
operation = request.GET['operation']
pid=request.GET['id']
if "delete_node" == operation:
Buildkb4.objects.filter(pk=pid).delete()
elif 'text' in request.GET:
nodename=request.GET['text']
if "create_node" == operation:
p=Buildkb4.objects.create(parent=pid,created_by=cu_id,text=nodename)
elif "rename_node" == operation:
Buildkb4.objects.filter(pk=pid).update(text=nodename)
return render(request,"registration/basetest.html")
现在可以使用了。 但是 GET 应该没有 副作用。所以你基本上违反了 GET 请求的约定。
通常通过 POST 或 DELETE 请求执行删除,而非 GET 请求。更改数据也由 POST 或 PATCH 请求处理,同样是 而不是 GET 请求。通过发出带有副作用的 GET 请求,机器人(如搜索引擎)可能会查询 URL,并删除 and/or 更新您的数据。这可能不是你想要的。
以下是来自用户的三个请求,create_node、rename_node 工作正常但 delete_node 请求无效。
requests:
[31/Aug/2018 17:00:53] "GET /buildknowledge/?operation=create_node&id=1&text=New+node HTTP/1.1" 200 16071
[31/Aug/2018 17:01:10] "GET /buildknowledge/?operation=rename_node&id=63&text=pkkkk HTTP/1.1" 200 16071
[31/Aug/2018 17:02:33] "GET /buildknowledge/?operation=delete_node&id=63 HTTP/1.1" 200 16071
views.py:
def basetest(request):
cu_id = request.user.id
if 'operation' in request.GET and "id" in request.GET and "text" in request.GET:
operation = request.GET['operation']
pid=request.GET['id']
nodename=request.GET['text']
if "create_node" == operation:
p=Buildkb4.objects.create (parent=pid,created_by=cu_id,text=nodename)
p.save()
elif "delete_node" == operation:
Buildkb4.objects.filter(pk=pid).delete()
elif "rename_node" == operation:
Buildkb4.objects.filter(pk=pid).update(text=nodename)
return render(request,"registration/basetest.html")
Note: It is not a good idea to use GET requests to perform side-effects (like updating or removing records in a database). Therefore I would advice to change such requests to a POST, DELETE or PATCH. More information at the bottom of this post.
根据您显示的日志,删除调用在 querystring 中带有两个参数:operation
和 id
。但是第一个 if
语句还需要一个 text
参数:
def basetest(request):
cu_id = request.user.id
<b>if 'operation' in request.GET and "id" in request.GET and "text" in request.GET:</b>
operation = request.GET['operation']
pid=request.GET['id']
nodename=request.GET['text']
if "create_node" == operation:
p=Buildkb4.objects.create (parent=pid,created_by=cu_id,text=nodename)
p.save()
elif "delete_node" == operation:
Buildkb4.objects.filter(pk=pid).delete()
elif "rename_node" == operation:
Buildkb4.objects.filter(pk=pid).update(text=nodename)
return render(request,"registration/basetest.html")
所以这永远不会达到 elif "delete_node" == operation:
的情况。
您可以通过减少检查限制来解决此问题,例如:
def basetest(request):
cu_id = request.user.id
if 'operation' in request.GET and "id" in request.GET:
operation = request.GET['operation']
pid=request.GET['id']
if "delete_node" == operation:
Buildkb4.objects.filter(pk=pid).delete()
elif 'text' in request.GET:
nodename=request.GET['text']
if "create_node" == operation:
p=Buildkb4.objects.create(parent=pid,created_by=cu_id,text=nodename)
elif "rename_node" == operation:
Buildkb4.objects.filter(pk=pid).update(text=nodename)
return render(request,"registration/basetest.html")
现在可以使用了。 但是 GET 应该没有 副作用。所以你基本上违反了 GET 请求的约定。
通常通过 POST 或 DELETE 请求执行删除,而非 GET 请求。更改数据也由 POST 或 PATCH 请求处理,同样是 而不是 GET 请求。通过发出带有副作用的 GET 请求,机器人(如搜索引擎)可能会查询 URL,并删除 and/or 更新您的数据。这可能不是你想要的。