Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

python 3.x - How to use PermissionRequiredMixin in FBV?

I am thinking about how I can use PermissionRequiredMixin in FBV.(I have used the same in CBV and it is working as expected). Please find the FBV(here need to implement permission). I don't want to use @login_required()

@login_required() This will check only if the user is authenticated or not.

def delete_viewgen(request,PermissionRequiredMixin,id):
    oneuser=ShiftChange.objects.get(id=id)
    permission_required = ('abc.add_shiftchange',)
    oneuser.delete()# delete
    return redirect('/gensall')

I am getting ERROR : missing 1 required positional argument: 'PermissionRequiredMixin'

CBV Case where it is working fine.

class ShiftChangeUpdateView(PermissionRequiredMixin,UpdateView):
    permission_required = ('abc.change_shiftchange',)
    login_url = '/login/'
    model=ShiftChange
    fields='__all__'

In CBV it is working fine and if user is not having change permission it is giving 403 Forbidden how to implement same in FBV and also how to customize 403 Forbidden message.

Thanks!

question from:https://stackoverflow.com/questions/65650217/how-to-use-permissionrequiredmixin-in-fbv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On function based views you would use decorators, in your particular case permission_required decorator

@permission_required('abc.change_shiftchange', raise_exception=True)
delete_viewgen(request,id)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...