Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

missing 1 required positional argument: 'get_response'

Tags:

django

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?

like image 449
anderish Avatar asked Feb 19 '26 10:02

anderish


1 Answers

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.

like image 179
Alasdair Avatar answered Feb 21 '26 13:02

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!