如果在基于函数的视图上permission_required失败,如何引发错误消息?我想引发错误消息,而不是重定向到登录页面,我知道如何在CBV中引发错误消息,但我不知道如何引发它在基于函数的视图中,请帮助我。 谢谢,这是一个代码。
@permission_required('request.user.projectuser.change_projectusermodel')
def update(request, id):
test = User.objects.get(id=id)
try:
second_form = UpdateAccountDetailForm(request.POST or None, instance=test.profile)
except:
second_form = UpdateAccountDetailForm(request.POST or None)
try:
third_form = UserRoleForm(request.POST or None, instance=test.roles)
except:
third_form = UserRoleForm(request.POST or None)
if request.method == 'POST':
form = UsersForm(request.POST, instance=test)
form2 = second_form
form3 = third_form
else:
form = UsersForm(instance=test)
form2 = second_form
form3 = third_form
return save_all(request, form, form2, form3,'projectuser/update.html')
答案 0 :(得分:1)
documentation section on permission_required指定了操作方法:您必须在启用了raise_exception
标志的情况下调用它:@permission_required(..., raise_exception=True)