python - Adding an argument to a decorator -
I have this decorator, when I use to decorate the digenone scene, share is the argument < Code> True (Controlled by middleware)
class no_share (object): def __init __ (see itself): self.view = view def __Call __ (self, request, * Args, ** kwargs): "if" kwargs.get ('shared' is true): "do not vote if you share" Django.http to import HTTP 404 HTTP 404 'to share' Not available ') returns self.view (un Corrupted, * Args, ** kwargs)
It currently works like this:
@no_share def prefs (request, [.. .])
But I want to extend a little functionality so that it works like this:
@no_share ('prefs') def prefs (Request, [...])
My question is how can I modify this decorator class so that it can accept additional logic?
I hope articles by Bruce Eckell It helps.
Update: Your code for the article will look like this:
class no_share (object): def __init __ (self, arg1): self .arg1 = arg1 def __call __ (self, f): "" "If they are shared, do not tell them." Print 'Decorator Arguments:', self .arg1 def wrapped_f (request, * args, ** kwargs): if kwargs.get ('share' is true): django.http extract html 404 html 404 from import ('not available for sharing' as desired) To use in: @no_share ('prefs') def prefs (request, [...])
Comments
Post a Comment