Wednesday, April 17, 2019

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


Hi,

Can anyone help on this. I want to pass value of "USERROLE" across all the django templates through "base.html". How to efficiently call custom context processor as it queries database for every page load.

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/c18cac8e-ad16-412e-a557-f1f97f06a12b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment