Monday, June 10, 2019

Re: Lazy evaluation of django custom context processor in base.html


Hi, 

Can anyone help with django custom context processors. Have written above custom context processor that retrieves a value from database. This value has to be available in all templates as I have to pass it in "custom dimension" to google analytics. Is there an efficient way to pass this value to templates without hitting database query on every hit.

Thanks,
Bijal

On Monday, April 15, 2019 at 6:35:15 PM UTC+5:30, BIJAL MANIAR wrote:

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/eae07c13-8658-49b9-a2ba-1ee9ab12c1f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment