[英]Django REST framework - “Method \”GET\“ not allowed.” -
In creating Django REST framework, i'll get all the data using this code在创建 Django REST 框架时,我将使用此代码获取所有数据
views.py视图.py
@api_view(['GET', ])
def api_detail_educationlevel(request):
if request.method == 'GET':
educationlevel = EducationLevel.objects.all()
serializer = EducationLevelSerializer(educationlevel, many=True)
return Response(serializer.data)
urls.py网址.py
path('api/', views.api_detail_educationlevel),
but when i add in my views.py like this但是当我像这样添加我的views.py时
@api_view(['PUT', ])
def api_update_educationlevel(request, pk):
try:
educationlevel = EducationLevel.objects.get(pk=pk)
except EducationLevel.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'PUT':
serializer = EducationLevelSerializer(educationlevel, data=request.data)
data = {}
if serializer.is_valid():
serializer.save()
data["success"] = "update successfull"
return Response(data=data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['DELETE', ])
def api_delete_educationlevel(request, pk):
try:
educationlevel = EducationLevel.objects.get(pk=pk)
except EducationLevel.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'DELETE':
operation = educationlevel.delete()
data ={}
if operation:
data["success"] = "delete successfull"
else:
data["failure"] = "delete failed"
return Response(data=data)
@api_view(['POST', ])
def api_create_blog_view(request):
authentication_classes = [SessionAuthentication, BasicAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request, format=None):
content = {
'user': unicode(request.user), # `django.contrib.auth.User` instance.
'auth': unicode(request.auth), # None
}
return Response(content)
and in my urls.py在我的 urls.py
urlpatterns = [
path('api/', views.api_detail_educationlevel),
path('api/update/', views.api_update_educationlevel),
path('api/delete/', views.api_delete_educationlevel),
path('api/create/', views.api_create_blog_view),
]
urlpatterns = format_suffix_patterns(urlpatterns)
I dont know why i am getting this message,我不知道为什么我会收到此消息,
in my path('api/update/', views.api_update_educationlevel),在我的路径中('api/update/',views.api_update_educationlevel),
in my path('api/delete/', views.api_delete_educationlevel),在我的路径中('api/delete/',views.api_delete_educationlevel),
in my path('api/create/', views.api_create_blog_view),在我的路径中('api/create/',views.api_create_blog_view),
any idea why i am getting this message?知道为什么我会收到此消息吗? i just want functional rest framework that can update/delete/insert data using user auth account我只想要可以使用用户身份验证帐户更新/删除/插入数据的功能 rest 框架
For each of these views ( views.api_detail_educationlevel, views.api_update_educationlevel, views.api_delete_educationlevel, views.api_create_blog_view
), you define @api_view(['*api method*', ])
.对于每个视图( views.api_detail_educationlevel, views.api_update_educationlevel, views.api_delete_educationlevel, views.api_create_blog_view
),您定义@api_view(['*api method*', ])
。
Only the views.api_detail_educationlevel
has @api_view(['GET', ])
therefore allowing a GET method.只有views.api_detail_educationlevel
有@api_view(['GET', ])
因此允许 GET 方法。 The others don't.其他人没有。 Either add a GET method to the other views or, like the documentation you follow, create a class containing each method.将 GET 方法添加到其他视图,或者像您遵循的文档一样,创建包含每个方法的 class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.