I want to have a TemplateView Class that uses LoginRequiredMixin and UserPassesTestMixin at the same time. Something like this:
from django.views.generic import TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
class FinanceOverview(LoginRequiredMixin, UserPassesTestMixin, TemplateMixin):
login_url = '/login'
redirect_field_name = 'next'
def test_func(self):
return self.request.user.groups.filter(name="FinanceGrp").exists()
def get(self, request, *args, **kwargs):
DO SOMETHING IF USER IS AUTHENTICATED AND ALSO MEMBER OF GROUP FinanceGrp
Run Code Online (Sandbox Code Playgroud)
Basically as you can see above, what I want to achieve is the following:
If user is not authenticated, to redirect …
我想创建一个 Mixin,它将:首先 - 检查用户是否通过身份验证,如果没有,重定向到登录 url。如果是... 第二 - 检查用户是否有定义的配置文件(用户),如果没有,重定向到配置文件创建,否则,允许用户访问视图。
我打算做一些类似的事情:
class ProfileRequiredMixin(LoginRequiredMixin,PermissionRequiredMixin):
#TODO check how multiple inheritance works treating conflicting methods
'''This Mixin should first check if user is autheticated, if not,
redirect to login. If it is, check if it has a profile.
If it does not, redirect to profile creation url. If it has, allow
access to view.'''
pass
Run Code Online (Sandbox Code Playgroud)
但我对如何覆盖handle_no_permission()和dispatch()方法感到困惑。