anderish
anderish

Reputation: 1759

missing 1 required positional argument: 'get_response'

So I am using Django 1.11. I used to use Django 1.9 and I remembered writing this piece of login middleware.

import re

from django.conf import settings
from django.shortcuts import redirect

EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
    EXEMPT_URLS += [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS]

class LoginRequired:
    def __init__(self, get_response):
        self.get_response = get_response(request)

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_view(self, request, view_func, view_args, view_kwargs):
        assert hasattr(request, 'user')
        path = request.path_info.lstrip('/')
        if not request.user.is_authenticated():
            if not any(url.match(path) for url in EXEMPT_URLS):
                return redirect(settings.LOGIN_URL)

However, I think something changed but I'm not sure what. I get the error:__init__() missing 1 required positional argument: 'get_response'

Any ideas?

Upvotes: 2

Views: 3118

Answers (1)

Alasdair
Alasdair

Reputation: 309089

You have written a new style middleware which will work in Django 1.10+. It will not work with old style middleware.

Make sure that you have defined MIDDLEWARE instead of MIDDLEWARE_CLASSES in your settings, so that Django treats your middleware as new-style middleware.

Upvotes: 11

Related Questions