Monday, April 15, 2019

Lazy evaluation of django custom context processor in base.html


Hi,

Have created a custom context processor to retrieve extra user attribute from custom UserData model. Passing this variable in "base.html" template calls the custom context processor function on every request to render html page. Since it is a database query, how to efficiently call custom context processor in "base.html".

custom_context_processor.py
def global_settings_userrole(request):
    USERROLE = None
    if request.user.is_authenticated():
        u = UserData.objects.filter(username=request.user.username).values('role').first()
        USERROLE = u['role']
    return {
        'USERROLE': USERROLE
    }





Thanks,
Bijal

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55ecb39c-94dc-4782-8c60-927af7d5af88%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment