Django 1.8 How do I add `sensitive_variables` or `sensitive_post_parameters` to a FormView method?
Question: When debugging an error with a form submit in Django, I noticed that the
user's password is in plain view in the debug "Request information" readout as
part of the POST parameters.

How do I wrap the `form_valid` (or maybe `dispatch`?) so that
`POST['password']` is hidden from the debugging information? I can't seem to
find the right combination of `@method_decorator` etc.

<https://docs.djangoproject.com/en/1.8/howto/error-reporting/#filtering-
sensitive-information>

    
    
    from django.utils.decorators import method_decorator
    from django.views.decorators.debug import sensitive_variables, sensitive_post_parameters
    
    class ActivateView(FormView):
    
        form_class = ActivatePasswordForm
        template_name = 'activate.html'
    
        def form_valid(self, form):
    
            # erroneous function which has been fixed
            do_something(form.cleaned_data['password'])
    
            return super().form_valid(form)
    

I have tried:

    
    
        @method_decorator(sensitive_variables)
        def form_valid(self, form):
    

and:

    
    
        @method_decorator(sensitive_post_parameters)
        def form_valid(self, form):
    

but both bail out at:

    
    
    Traceback:
    File "venv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
      223.                 response = middleware_method(request, response)
    File "venv/lib/python3.5/site-packages/django/middleware/clickjacking.py" in process_response
      31.         if response.get('X-Frame-Options', None) is not None:
    
    Exception Type: AttributeError at /activate/
    Exception Value: 'function' object has no attribute 'get'
    

and I've tried:

    
    
    @method_decorator(sensitive_variables)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)
    

and:

    
    
    @method_decorator(sensitive_post_parameters)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)
    

but both bail out at:

    
    
    Traceback:
    File "venv/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
      132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "venv/lib/python3.5/site-packages/django/views/generic/base.py" in view
      71.             return self.dispatch(request, *args, **kwargs)
    File "venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
      34.             return bound_func(*args, **kwargs)
    
    Exception Type: TypeError at /activate/
    Exception Value: decorator() got an unexpected keyword argument 'unique_id'
    

This is the type error mentioned in [the
docs](https://docs.djangoproject.com/en/1.8/topics/class-based-
views/intro/#decorating-the-class).

## Solved

Solution is a mixture of the two answers below from Dave and Alasdair. Thanks.

The `@sensitive_post_parameters` decorator will only take effect when
`DEBUG=False`, which explains why I wasn't seeing anything change. Also, the
method has to be called within the `@method_decorator`. So the correct code
is:

    
    
    @method_decorator(sensitive_post_parameters('password', 'password_again'))
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)
    


Answer: `sensitive_variables` is documented as a decorator rather than an argument to
a decorator. And even with the correct syntax, it's possible but unlikely that
"treated in a special way" means that the variables will not be visible in
debugging information.

Production servers should never be run with DEBUG=True, and sensitive
variables should never appear in any logs generated by a production server.
But when debugging, the goal is to have all the information necessary to track
down problems, which includes passwords. Let us know if the debug page
generator censors sensitive variables. That would be a surprise.

    
    
    def sensitive_variables(*variables):
    """
    Indicates which variables used in the decorated function are sensitive, so
    that those variables can later be treated in a special way, for example
    by hiding them when logging unhandled exceptions.
    
    Two forms are accepted:
    
    * with specified variable names:
    
        @sensitive_variables('user', 'password', 'credit_card')
        def my_function(user):
            password = user.pass_word
            credit_card = user.credit_card_number
            ...
    
    * without any specified variable names, in which case it is assumed that
      all variables are considered sensitive:
    
        @sensitive_variables()
        def my_function()
            ...
    """
    

