Chase Roberts
Chase Roberts

Reputation: 9376

Django process_response() takes 2 positional arguments but 3 were given

I am trying to write some middleware for my django app. In the docs it defines process response with two parameters, request and response. But when I try to write some middleware I get an exception thrown:

Type Error:
process_response() takes 2 positional arguments but 3 were given

Here is my code:

from django.http import HttpResponseRedirect

class Middleware(object):

    def process_response(self, request, response):
        print('processing response')
        ref = request.GET.get('ref','')
        if ref:
            response.set_cookie('ref', ref, max_age=3600*24*365)
        return response

    def process_request(request):
        print('processing request')
        print(request.META['PATH_INFO'])
        ref = request.COOKIES.get('ref','')
        if ref:
            request.GET['ref'] = ref
        return None

But then a peculiar thing happened. I commented out the process_request function. And then I got 0 errors. When I uncommented the process_request function I then got the error:

process_request() takes 1 positional argument but 2 were given

This is starting to seem like random behavior..

Upvotes: 1

Views: 6754

Answers (2)

bearcat
bearcat

Reputation: 884

I got here following the error message "takes 2 positional arguments but 3 were given" although in my case, I'm actually providing 2 positional arguments and 3 are desired. This is for anyone else who is in the same situation. Django 2.2.5 for me.

change

def has_add_permission(self, request):
    return False

to:

def has_add_permission(self, request, obj=None):
    return False

I probably could have found this by looking at the django source, instead I found it here:

https://github.com/celiao/django-rest-authemail/issues/32#issuecomment-800859698

Upvotes: 1

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

You're missing self in the process_request definition.

Upvotes: 4

Related Questions