In my app, I have two different login pages according the user that need to log in. So I cannot use settings.LOGIN_URL.
I am also using class-based views, so I have to use method_decorator(login_required).
At this moment, I have a class decorator (I am using Python 2.7) that does pretty much all what I want, except for the login_url. Code is a snippet and hand-typed, so it may have typo.
login_required_m = method_decorator(login_required)
def requires_role1(view):
def requires_m(func):
@wraps(func)
def wrapper(self, request, *args, **kwargs):
# I need to process request.user
if (not ok):
return redirect(urlresolvers.reverse('some_name'))
else:
f = login_required_m(func)
return f(self, request, *args, **kwargs)
return wrapper
view.get = requires_m(view.get)
view.post = requires_m(view.post)
return view
The line that really concerns me is:
`f = login_required_m(func)`
On a function based view, I would have been able to define:
`f = login_required(func, login_url='some_reverse_path')`
but with method_decorator(login_required), I am no longer able to pass login_url as a parameter. Remember, I need to be able to do so because I have two different login urls. They provide totally different sets of usernames and passwords (same username can be in both set with different passwords and associated to different people).
Any help in what I should do to accomplish this would be much appreciated.
EDIT
Accorded to accepted answer, here is the solution I adopted:
def login_required_partial(func):
return partial(login_required, login_url=urlresolvers.reverse('role1_login'))
login_required_m = method_decorator(login_required_partial)
and left the rest of the code unchanged. And it all seems to work well when there are no parameters passed by ulr(r'path/(?P<param1>.*)', CBV.as_view(), name='path_name').
Anyone with a good way to fix this now?
You can decorate the CBV view function:
Or you can create some sort of a mixin:
And use it like so: