Добрый день, как работает декоратор в джанго?
Есть некая ф-ция
@csrf_exempt
@ratelimit
def user_login(request):
if request.method == 'POST':
data = req_data(request)
Вариант №1
def ratelimit(func):
def wrapper():
print('Code before func')
func()
print('Code after func')
return wrapper
Internal Server Error: /api/v1/user/login
Traceback (most recent call last):
File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/maxim/buzz/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
TypeError: wrapper() takes 0 positional arguments but 1 was given
Вариант №2
def ratelimit(func):
def wrapper(*args):
print(args)
print('Code before func')
func()
print('Code after func')
return wrapper
Передается запрос, но теперь возникает другая ошибка
(<WSGIRequest: POST '/api/v1/user/login'>,)
Code before func
Internal Server Error: /api/v1/user/login
Traceback (most recent call last):
File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/maxim/buzz/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/maxim/PycharmProjects/back_new/app/back/api/utils/ratelimit.py", line 6, in wrapper
func()
TypeError: user_login() missing 1 required positional argument: 'request'
[28/Aug/2017 06:51:19] "POST /api/v1/user/login HTTP/1.1" 500 72325
Что именно не верно в данном примере?
Вторая ошибка(<WSGIRequest: POST '/api/v1/user/login'>,)
Code before func
Code after func
Internal Server Error: /api/v1/user/login
Traceback (most recent call last):
File "/home/maxim/buzz/lib/python3.5/site-packages/django/core/handlers/base.py", line 158, in get_response
% (callback.__module__, view_name))
ValueError: The view api.utils.ratelimit.wrapper didn't return an HttpResponse object. It returned None instead.
[28/Aug/2017 07:00:33] "POST /api/v1/user/login HTTP/1.1" 500 59845
Например мне не нужно чтобы декоратор отдавал ответ, а дальше продолжала выполняться ф-ция user_login, как быть в таком случаи?