"Method \"GET\" not allowed."

I really need help explaining why I pass the method as DELETE but the error appears "Method "GET" not allowed." Thank you very much.

class DeleteUserByUsernameView(APIView):
    def delete(self, request, username):
        try:
            instance = User.objects.filter(username=username)
            instance.delete()
            return HttpResponse("Success")
        except Exception as e:
            return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
from django.contrib import admin
from django.urls import path, include
from . import views


urlpatterns = [
    path('delete/user/<str:username>/', views.DeleteUserByUsernameView.as_view()),
    path('register/', views.RegisterView.as_view(), name="Register"),
    path('login/', views.LoginView.as_view(), name="Login"),

]

enter image description here

enter image description here enter image description here

Your path has a trailing slash:

#          trailing slash 🖟
delete/user/<str:username>/

so that means you need to work in Postman with:

#                    🖟
/delete/user/quyhoang/

If you don't do that, and the APPEND_SLASH setting is set to True (which is the default), Django will, if there is no trailing slash, and no path matches, automatically return a redirect (HTTP response 301) to the path with the slash, as a result Postman makes a GET request to that path.

But the solution is thus to make a DELETE request to the path with the slash from the start. Since a redirect always is handled as GET request, so it essentially "strips off" the original HTTP method.g

Back to Top