Answer the question
In order to leave comments, you need to log in
How to write custom decorator in Django views?
Good afternoon!
There is a code that is used to check user authorization (I know that there is a standard functionality, but it needs to be custom)
def decorator_login_check(func):
def wrapper(request):
try:
user = User.objects.get(id=request.session['loged_user'])
except Exception as ex:
return redirect('auth_login')
func(request)
return wrapper
@decorator_login_check
def index(request):
""" Главная страница """
user = User.objects.get(id=request.session['loged_user'])
context = {
'user': user
}
return render(request, 'main/index.html', context)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question