How to effectively use PUT and DELETE HTTP methods in Django Class-Based Views?

126 views
Skip to first unread message

Mamadou Alpha Bah

unread,
Apr 12, 2024, 1:20:11 PM4/12/24
to Django users

I'm setting up a CRUD system with Django, using Class-Based Views. Currently I'm trying to figure out how to handle HTTP PUT and DELETE requests in my application. Despite searching the Django documentation extensively, I'm having trouble finding concrete examples and clear explanations of how to submit these types of queries to a class-based view.

I created a view class named CategoryView, extending from: django.views.View, in which I implemented the get and post methods successfully. And I want to build my urls like this:

  1. New Category: 127.0.0.1:8000/backendapp/categories/create
  2. List all Category: 127.0.0.1:8000/backendapp/categories/
  3. Retrieve only one Category: 127.0.0.1:8000/backendapp/categories/1
  4. Etc...

However, when I try to implement the put and delete methods, I get stuck.

For example :

from django.views import View 

class CategoryView(View):
     template_name = 'backendapp/pages/category/categories.html'
     
     def get(self, request):
          categories = Category.objects.all()
          context = { 'categories': categories }
          return render(request, self.template_name, context)

     def post(self, request):
          return

     def delete(self, request, pk):
          return

     def put(self, request):
          return

I read through the Django documentation and found that Class-Based Views support HTTP requests: ["get", "post", "put", "patch", "delete", "head ", "options", "trace"].

link: https://docs.djangoproject.com/en/5.0/ref/class-based-views/base/#django.views.generic.base.View

Despite this, I can't figure out how to do it.

So I'm asking for your help to unblock me.

I looked at the Django documentation and searched online for examples and tutorials on handling HTTP requests in class-based views. I also tried experimenting with adding the put and delete methods to my CategoryView view class, but without success. I expected to find resources that clearly explain how to integrate these queries into my Django application, as well as practical examples demonstrating their use. However, I haven't found a working solution and am now seeking help from the community to overcome this difficulty.

Luis Zárate

unread,
Apr 12, 2024, 10:14:17 PM4/12/24
to django...@googlegroups.com
Also on view you have a variable called, where you can limit the available methods.
http_method_names

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b1ecd4a7-5946-4da5-80ae-5923b6648a70n%40googlegroups.com.


--
"La utopía sirve para caminar" Fernando Birri


Luis Zárate

unread,
Apr 12, 2024, 10:16:53 PM4/12/24
to django...@googlegroups.com
Also a recomendation, see that `dispatch` method call the view as
handler(request, *args, **kwargs)
So maybe is a good idea do something like:

class MyView(view):
     def put(self, request, *args, **kwargs):
            ...


Regards,

Ryan Nowakowski

unread,
Apr 14, 2024, 6:23:42 PM4/14/24
to django...@googlegroups.com
Check out https://ccbv.co.uk/projects/Django/5.0/django.views.generic.base/View/

It's super useful in understanding class based views.

What you're doing should work though. What error are you seeing?
Reply all
Reply to author
Forward
0 new messages