Delete Ajax (Deleteview)

When sending the request via ajax to delete a user, I get the following errors.

Browser console errors:

image

django server errors:

DeleteView uses FormMixin to handle POST requests. As a consequence, any custom deletion logic in EliminarUsuario.delete() handler should be moved to form_valid().
  self = cls(**initkwargs)
[27/Feb/2023 13:17:58] "GET /usuarios/eliminar_usuario/7/ HTTP/1.1" 200 837
Internal Server Error: /usuarios/eliminar_usuario/7/
Traceback (most recent call last):
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\edit.py", line 278, in post
    return self.form_valid(form)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\edit.py", line 283, in form_valid
    success_url = self.get_success_url()
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\apenaranda\Desktop\Python_Intranet\env\Lib\site-packages\django\views\generic\edit.py", line 240, in get_success_url
    raise ImproperlyConfigured("No URL to redirect to. Provide a success_url.")
django.core.exceptions.ImproperlyConfigured: No URL to redirect to. Provide a success_url.
[27/Feb/2023 13:18:01] "POST /usuarios/eliminar_usuario/7/ HTTP/1.1" 500 91033

view.py

class EliminarUsuario(DeleteView):
    model = Usuario
    template_name = 'usuarios/eliminar_usuario.html'

    def delete(self,request,*args,**kwargs):
        request.headers.get('x-requested-with') == 'XMLHttpRequest'
        if request.headers.get('x-requested-with') == 'XMLHttpRequest':
            usuario = self.get_object()
            usuario.usuario_activo = False
            usuario.save()
            mensaje = f'{self.model.__name__} eliminado correctamente!'
            error = 'No hay error!'
            response = JsonResponse({'mensaje': mensaje, 'error': error})
            response.status_code = 201
            return response
        else:
            return redirect('usuarios:inicio_usuarios')

index.js

function eliminar(pk){
    $.ajax({
        data:{
            csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val()
        },
        url: '/usuarios/eliminar_usuario/'+pk+'/',
        type: 'post',
        success: function (response) {
            notificacionSuccess(response.mensaje);
            listadoUsuarios();
            cerrar_modal_eliminacion();
        },
        error: function (error) {
            notificacionError(error.responseJSON.mensaje);
        }
    });
}

I don’t know where the problem is…

The error message is rather straight-forward:

DeleteView uses FormMixin to handle POST requests. As a consequence, any custom deletion logic in EliminarUsuario.delete() handler should be moved to form_valid().

django.core.exceptions.ImproperlyConfigured: No URL to redirect to. Provide a success_url.

Also see Django 4.0 release notes | Django documentation | Django