I am explicitly studying the following decorator
#decorators.py
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
def auth_check(view_func):
def _wrapped_view_func(request, *args, **kwargs):
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('pets:home'))
return view_func(request, *args, **kwargs)
return _wrapped_view_func
I few a few questions regarding this decorator and decorators in general.
How do they know to only be applied to the code directly below them? Also, it is usually the case that code will get an error if using parameters that aren't defined yet (code underneath in the script)
What exactly is *args and **kwargs doing? I realize they stand for argument variables, but why do you need two of them? Would you need one for each possible variable? How does this work?
Why are we "wrapping" the view function? I know thats just a name, but why is it called a wrapper in this case? Is this usually how its done?