I wrote my decorator to check for authorization, since it is the request that needs to be received, not request.user, and to check for data in the request session:

def login_required(func=is_authenticated, redirect_url="/"): def decorator(view_func): @wraps(view_func, assigned=available_attrs(view_func)) def wrapper(request, *args, **kwargs): if func(request): return view_func(request, *args, **kwargs) else: return HttpResponsePermanentRedirect(redirect_url) return wrapper return decorator 

If in views (controllers)

 @login_required(is_authenticated) def start(request) ... 

In a call to the decorator to specify a specific check function (the same as the default), then everything works. If otherwise:

 @login_required def start(request) ... 

An exception is thrown:

 Environment: Request Method: GET Request URL: http://127.0.0.1:8000/start/ Django Version: 2.0.6 Python Version: 3.5.2 Installed Applications: ['web', 'bootstrap4', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware'] Traceback: File "/home/mister/Рабочий стол/COMPANY/PROJECT/ENV/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner 35. response = get_response(request) File "/home/mister/Рабочий стол/COMPANY/PROJECT/ENV/lib/python3.5/site-packages/django/utils/deprecation.py" in __call__ 97. response = self.process_response(request, response) File "/home/mister/Рабочий стол/COMPANY/PROJECT/ENV/lib/python3.5/site-packages/django/middleware/clickjacking.py" in process_response 26. if response.get('X-Frame-Options') is not None: Exception Type: AttributeError at /start/ Exception Value: 'function' object has no attribute 'get' 

I ask you to suggest what this behavior is connected with and how to solve this problem, if there is such an opportunity, without specifying the verification function to the decorator at each call

PS In python && django recently, so if this is a very simple question, please do not throw slippers, I searched and searched, but I could not figure it out

  • the @login_required(is_authenticated) will result in the following login_required(is_authenticated)(start) . the second option @login_required will result in login_required(start) . Add @login_required() brackets. - godva pm
  • @godva could you tell me where you can read about this? a direct link would be very useful :) Thanks for the advice, I ’m trying out how to get to the PC - Aiswe
  • pep-318 - godva
  • @godva add your comment with a link to the doc, I will mark it as a solution. - Aiswe

1 answer 1

Writing @login_required(is_authenticated) will result in the following login_required(is_authenticated)(start) . The second option @login_required приведет к login_required(start) . Add @login_required() brackets. Read more about the syntax of decorators read in pep-318 .