我正在使用Django-REST-Framework和Apache Cassandra构建示例REST API。 这是我的模型:
import uuid
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
class User(Model):
read_repair_chance = 0.05
user_id = columns.UUID(primary_key=True, default=uuid.uuid4)
username = columns.Text(required=True)
password = columns.Text(required=True)
email = columns.Text(required=True)
def __str__(self):
return self.username
def get(self,request, format=None):
uname = [user.name for user in User.object.all()]
return Response(uname)
我能够添加新用户并获得他们的列表。
但是当我尝试使用DELETE
发出POSTMAN
请求时,
DELETE http://localhost:8000/users/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce
我正在获取找不到页面404错误。 有人可以确定我要去哪里错了。 这是其余文件的内容: Views.py
from rest_framework import viewsets
from .models import User
from .serializers import UserSerializer
from rest_framework.response import Response
from rest_framework import status
class UserView(viewsets.ViewSet):
def get_object(self, pk):
try:
return User.objects.get(pk=pk)
except User.DoesNotExist:
raise Http404
def get_user_list(self, request):
queryset = User.objects.all()
serializer = UserSerializer(queryset,many=True)
return Response(serializer.data)
def get(self, request, pk, format=None):
user = self.get_object(pk)
serializer = UserSerializer(user)
return Response(user.data)
def post(self, request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None):
user = self.get_object(pk)
serializer = UserSerializer(user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None):
user = self.get_object(pk)
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
项目URL:
from django.urls import path, include
urlpatterns = [
path('users/', include('userdatabase.urls')),
#path('',)
]
from django.urls import path, include
from .views import UserView
user_list = UserView.as_view({'get':'get_user_list', 'delete': 'delete'})
urlpatterns = [
path('', user_list, name='user_list'),
path('users/delete/<uuid:pk>', user_list, name='user_list')
]
serializers.py
import uuid
from rest_framework import serializers
from .models import User
class UserSerializer(serializers.Serializer):
user_id = serializers.UUIDField(default=uuid.uuid4())
username = serializers.CharField(required=True)
password = serializers.CharField(required=True)
email = serializers.CharField(required=True)
def create(self, validated_data):
return User.create(**validated_data)
def update(self, instance, validated_data):
instance.username = validated_data.get('email', instance.username)
instance.password = validated_data.get('content', instance.password)
instance.email = validated_data.get('created', instance.email)
return instance
编辑
当我在DELETE
上使用POSTMAN
时,得到以下HTML
响应:
http://localhost:8000/users/delete/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Page not found at /users/delete/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce</title>
<meta name="robots" content="NONE,NOARCHIVE">
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
body * * { padding:0; }
body { font:small sans-serif; background:#eee; color:#000; }
body>div { border-bottom:1px solid #ddd; }
h1 { font-weight:normal; margin-bottom:.4em; }
h1 span { font-size:60%; color:#666; font-weight:normal; }
table { border:none; border-collapse: collapse; width:100%; }
td, th { vertical-align:top; padding:2px 3px; }
th { width:12em; text-align:right; color:#666; padding-right:.5em; }
#info { background:#f6f6f6; }
#info ol { margin: 0.5em 4em; }
#info ol li { font-family: monospace; }
#summary { background: #ffc; }
#explanation { background:#eee; border-bottom: 0px none; }
</style>
</head>
<body>
<div id="summary">
<h1>Page not found
<span>(404)</span>
</h1>
<table class="meta">
<tr>
<th>Request Method:</th>
<td>DELETE</td>
</tr>
<tr>
<th>Request URL:</th>
<td>http://localhost:8000/users/delete/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce</td>
</tr>
</table>
</div>
<div id="info">
<p>
Using the URLconf defined in
<code>usermanager.urls</code>,
Django tried these URL patterns, in this order:
</p>
<ol>
<li>
users/
[name='user_list']
</li>
<li>
users/
users/delete/<uuid:pk>
[name='user_list']
答案 0 :(得分:2)
您将delete
作为网址格式的一部分:
path('users/delete/<uuid:pk>', user_list, name='user_list')
然而,当您运行邮递员时,您使用的网址中没有delete
:
http://localhost:8000/users/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce
您应该改用以下网址:
http://localhost:8000/users/delete/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce
答案 1 :(得分:2)
使用
http://localhost:8000/users/delete/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce
代替
http://localhost:8000/users/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce
我还阅读了您的评论,基于以下原因,由于{strong>两个原因(或更多原因)引起的404
错误
1.输入的URL与任何已定义的URL不匹配
2.在与给定的UUID
相对应的数据库中找不到用户
因此,我建议在print()
方法内添加一个delete()
函数,以检查在API访问期间是否调用了该函数。
def delete(self, request, pk, format=None):
print("inside delete")
user = self.get_object(pk)
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
或者,您可以使用POSTMAN
或类似的工具检查响应
对于删除API,您应该使用
http://localhost:8000/users/users/delete/0f7b0ada-01ab-4907-b9d2-3fa0d27ae1ce
也就是说,我们都错过了api
/users
将
path('users/delete/<uuid:pk>', user_list, name='user_list')
更改为
path('users/delete/<uuid:user_id>', user_list, name='user_list')
这是因为为您提供了pk
的查找字段,但它应该是user_id