LoginRequiredMixin fired after user_passes_test

Hi,

I have a detail view:

class ReviewDetailView(LoginRequiredMixin,DetailView):
    redirect_field_name = None
    login_url ='/reviews/login'
    ....
    ....
    ....
	@method_decorator(user_passes_test(is_emp_or_manager,login_url='/reviews/unauthorized'))
	def dispatch(self, *args, **kwargs):
		return super(ReviewDetailView, self).dispatch(*args, **kwargs)

Now when I try to access the page without login it redirects to login_url=’/reviews/unauthorized’.

Is there a way to achieve loginmixin to fire first, other than using a login required decorator above user_passes_test.

It looks like LoginRequiredMixin primarily works by overriding dispatch.

Could you apply the user_passes_test on a different method, such as get_object?

Silly me - there’s another option that I should have remembered since it’s what we’ve done.

If you really want to catch this at the dispatch method, you could create your own mixin that either inherits from LoginRequiredMixin and calls super to invoke its dispatch method before calling the user_passes_test, or you create a completely separate Mixin that you add to the class definition after the LoginRequredMixin so that the dispatch method of LoginRequiredMixin calls your dispatch method via the super call.

Ken

Thanks Ken that works

1 Like