简体   繁体   English

Django REST 框架 - “方法 \”GET\“ 不允许。” -

[英]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.

相关问题 "detail": "方法 \\"GET\\" 不允许。" 在 TokenAuthentication Django 休息框架中 - "detail": "Method \"GET\" not allowed." in TokenAuthentication Django rest framework "detail": "方法 \"GET\" 不允许。" Django Rest 框架 - "detail": "Method \"GET\" not allowed." Django Rest Framework “详细信息”:“方法 \”GET\“ 不允许。” Django Rest 框架 - “detail”: “Method \”GET\“ not allowed.” Django Rest Framework DRF- Django Rest 框架 re_path 查询参数 - “方法 \”GET\“ 不允许。” - DRF- Django Rest Framework re_path query params - “Method \”GET\“ not allowed.” django“详细信息”:“方法\\”GET\\“不允许。” (一对多关系) - django “detail”: “Method \”GET\“ not allowed.” (one to many relationship) { "detail": "方法 \\"GET\\" 不允许。" } - { "detail": "Method \"GET\" not allowed." } “详细信息”:“方法 \\”GET\\” 不允许。在 Django 中调用端点 - “detail”: “Method \”GET\" not allowed. on calling endpoint in django DRF:“详细信息”:“方法\\” GET \\“不允许。” - DRF: “detail”: “Method \”GET\“ not allowed.” POST 返回“详细信息”:“不允许使用方法 \"GET\"。” - POST returns "detail": "Method \"GET\" not allowed." Django REST框架中的405“不允许方法POST” - 405 “Method POST is not allowed” in Django REST framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM