Using generic views with HTTP verbs

I am trying to mix HTTP verbs with Django's Built-in class-based generic views, i understand that is a good pattern (correct me if not) to keep Url's like app/pets/ and use HTTP's verbs to define what to do (GET, POST/create, PUT/update, DELETE/delete, etc) instead of something like app/pets/add or app/pets/delete as Django's documentation shows when using generic views with forms

So i tried something like this:

urls.py:

    path('login/', views.sign_in, name='login'),
    path('logout/', views.log_out, name='logout'),
    path('register/', views.register, name='register'),
    path('pets/', views.Pet.as_view(), name= "pets"),

Views.py

class Pet(LoginRequiredMixin, View):
    login_url= "/vet/login/"

    #handle Get Request
    class PetListView(ListView):
        model= models.Pet
        context_object_name= "pets"
        template_name= "vet/index.html"

    #Handle Put request
    class PetUpdateView():
        pass

but i am getting and error of 405 method not allowed (and i think is not a good practice do this).

i also tried this (defining the PetListView same as above):

class Pet(LoginRequiredMixin, View):
    login_url= "/vet/login/"

    def get(self, request):
        return HttpResponse(PetListView.as_view())

But this just print the object on the screen.

I am new to Django and i do not know what is the better approach to accomplish this using Django generic's views since it reduce a lot code for CRUD operations

Back to Top