python - Django. I need variables from my own context processor only when user is authenticated -
i have django 1.8.13. need variables own context processor when user authenticated. context processor:
def comment_rew(request): context_dict = {} if request.user.is_authenticated(): user = request.user user_rew = user.review_set.all().count() context_dict['user_rew'] = user_rew return(context_dict)
when user not authenticated returns empty dictionary , causes error(because context processor can't returns empty dict). possible use context processor if user authenticated?
context processors can't enabled/disabled based on authentication status of users.
you can instead set value of context variable user_rew
none
when users not authenticated:
def comment_rew(request): context_dict = {'user_rew': none} if request.user.is_authenticated(): user = request.user user_rew = user.review_set.all().count() context_dict['user_rew'] = user_rew return context_dict
Comments
Post a Comment