Here's my view. Basically, it returns different Responses based on whether it's logged in or not.
@check_login()
def home(request):
if is_logged_in(request):
return x
else:
return y
Here's my decorator code. I just want to check if the request has headers, and if so, log him in.
#decorator to log the user in if there are headers
def check_login():
def check_dec(func):
if request.META['username'] == "blah":
login(request, user)
return check_dec
The problem is..I don't know how to write a proper decorator in this case!!! What are the arguments? What are the functions? How?
Use only @check_login
instead of check_login()
- otherwise your decorator has to return a decorate as you are doing home = check_login()(home)
Here's an example decorator:
def check_login(method):
@functools.wraps(method)
def wrapper(request, *args, **kwargs):
if request.META['username'] == "blah"
login(request, user) # where does user come from?!
return method(request, *args, **kwargs)
return wrapper
This decorator will call execute your login function if the username field is set to "blah" and then call the original method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With