# Django Rest Framework 404 Not Found/PUT/DELETE

**URL:** <https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980>\
**Category:** Using Django\
**Created:** [May 20, 2021, 5:18pm UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980 "2021-05-20T17:18:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)\
**Post date:** [May 20, 2021, 5:18pm UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/1 "2021-05-20T17:18:04Z")

</div>

When using Django Rest Framework, how do we disable the `DELETE` button and `PUT` form from showing on the browsable API when a query returns a `404 Not Found`? Thank you.

 ![2021-05-20_13-13-51](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/4/48d95235231c3bbed6b63e91a1c26a42f947132e.jpeg)

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [May 20, 2021, 11:47pm UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/2 "2021-05-20T23:47:22Z")

</div>

The PUT and DELETE options may not have a relation with the `404 Not found`.

I assume you’re using a ModelViewSet, which provides the HTTP verbs (GET, POST, PUT, PATCH, DELETE).  
You can replace the ModelViewSet only with the mixins you need. Look at this example from the docs.  
[https://www.django-rest-framework.org/api-guide/viewsets/#example\_3](https://www.django-rest-framework.org/api-guide/viewsets/#example_3)

---

<div class="post-metadata">

**Author:** ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)\
**Post date:** [May 20, 2021, 11:50pm UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/3 "2021-05-20T23:50:23Z")

</div>

Thank you. I actually saw that example already. Is there a way to program the Serializer or View to not show the DELETE button and PUT form when there’s an invalid query that would produce a 404? Thank you again.

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [May 21, 2021, 12:11am UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/4 "2021-05-21T00:11:35Z")

</div>

Based on my assumption, you should need to exclude the mixins for PUT and DELETE.

```auto
class TaskViewset(
     mixins.CreateModelMixin, # POST
     mixins.ListModelMixin, # GET a list of instances
     mixins.RetrieveModelMixin, # GET a single instance
     viewsets.GenericViewSet
)

```

I recommend you to provide the code of your viewset so we can give better advice.

---

<div class="post-metadata">

**Author:** ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)\
**Post date:** [May 21, 2021, 12:14am UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/5 "2021-05-21T00:14:59Z")

</div>

I don’t want to exclude the PUT and DELETE when there is a valid response from a query, only when the query returns an invalid response such as 404.

```auto
class TaskViewSet(viewsets.ModelViewSet):
    queryset = Task.objects.all()
    serializer_class = TaskSerializer
    permission_classes = [
        permissions.IsAuthenticatedOrReadOnly,
        IsOwnerOrReadOnly
    ]

```

---

<div class="post-metadata">

**Author:** ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)\
**Post date:** [May 24, 2021, 6:05pm UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/6 "2021-05-24T18:05:16Z")

</div>

BTW, the same problem occurs in the Snippets example put out by Django Rest Framework as well. So, it’s something that is not addressed in the tutorial. IF anyone knows how to fix this, please share. Thank you in advance!

 ![2021-05-24_14-05-57](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/c/c00a249ca592efda99db00958c7d0f7419da93bb.jpeg)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 24, 2021, 6:21pm UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/7 "2021-05-24T18:21:37Z")

</div>

Couple different thoughts here:

- Django Rest Framework is not part of the Django project - there is [a separate support channel available for it](https://www.django-rest-framework.org/#support). You may get more direct substantial help through it.

- I don’t believe having a PUT option is an “error”. Quoting from [https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT:](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT:)

> The **HTTP `PUT` request method** creates a new resource or replaces a representation of the target resource with the request payload.

This means that having a PUT option for an entry that doesn’t exist would be appropriate for the creation of that entry.

---

<div class="post-metadata">

**Author:** ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)\
**Post date:** [May 24, 2021, 6:34pm UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/8 "2021-05-24T18:34:15Z")

</div>

Thank you. I will try the alternative channel. If I find a solution, may I still share it here?

---

<div class="post-metadata">

**Author:** ![mc613](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@mc613](https://forum.djangoproject.com/u/mc613)\
**Post date:** [June 1, 2021, 2:16am UTC](https://forum.djangoproject.com/t/django-rest-framework-404-not-found-put-delete/7980/9 "2021-06-01T02:16:24Z")

</div>

Here’s the solution I found that works, thanks to a bit of searching. In **renderers.py** file in the app directory, create a custom `BrowsableAPIRenderer` class and override `get_context()` and `get_rendered_html_form()`. The first method sets the context for display on the `PUT` HTML form. The latter method disables the `DELETE` button from showing.

```python
# renderers.py
from rest_framework.renderers import BrowsableAPIRenderer

class MyBrowsableAPIRenderer(BrowsableAPIRenderer):

    """
    Only render the browsable API if there is no 404 error
    """

    def get_context(self, *args, **kwargs):
        context = super().get_context(*args, **kwargs)

        response = args[2]['response']
        if response.status_code == 404:
            # do not display PUT form
            context['display_edit_forms'] = False

        return context

    def get_rendered_html_form(self, data, view, method, request):
       """
       {
          "detail": "Not found."
       }
       """
        if 'detail' in data:
            return
        return super().get_rendered_html_form(data, view, method, request)

```

In **settings.py** file, add these settings to use the default `JSONRenderer` and our custom `MyBrowsableAPIRenderer`:

```python
REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'planner.renderers.MyBrowsableAPIRenderer',
    ),
}

```

 ![FireShot Capture 091 - Person Instance – Django REST framework - localhost](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/4/449a106367ebb79e59681eaa30a9ad0ea470511d.png)

Hope this helps!!
