我希望在用户查看特定用户设置页面之前检查某些条件。我正在尝试使用user_passes_test装饰器来实现这一目标。该函数位于基于类的视图中,如下所示。我正在使用方法装饰器装饰视图中的get_initial函数。
class UserSettingsView(LoginRequiredMixin, FormView):
success_url = '.'
template_name = 'accts/usersettings.html'
def get_form_class(self):
if self.request.user.profile.is_student:
return form1
if self.request.user.profile.is_teacher:
return form2
if self.request.user.profile.is_parent:
return form3
@method_decorator(user_passes_test(test_settings, login_url='/accounts/usertype/'))
def get_initial(self):
if self.request.user.is_authenticated():
user_obj = get_user_model().objects.get(email=self.request.user.email)
if user_obj.profile.is_student:
return { ..........
...... ....
以下是test_settings函数:
def test_settings(user):
print "I am in test settings"
if not (user.profile.is_student or user.profile.is_parent or user.profile.is_teacher):
return False
else:
return True
我在使用装饰器时遇到了以下错误。
File "../django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "../django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "../braces/views.py", line 107, in dispatch
request, *args, **kwargs)
File "../django/views/generic/base.py", line 87, in dispatch
return handler(request, *args, **kwargs)
File "../django/views/generic/edit.py", line 162, in get
form = self.get_form(form_class)
File "../django/views/generic/edit.py", line 45, in get_form
return form_class(**self.get_form_kwargs())
File "../django/views/generic/edit.py", line 52, in get_form_kwargs
'initial': self.get_initial(),
File "../django/utils/decorators.py", line 29, in _wrapper
return bound_func(*args, **kwargs)
TypeError: _wrapped_view() takes at least 1 argument (0 given)
我不确定如何解决这个错误。我是否在错误的函数上应用了装饰器?任何线索都会有帮助。
is_authenticated()
被更改为is_authenticated
。 - bones225