Lazy evaluation of django custom context processor in base.html

81 views
Skip to first unread message

BIJAL MANIAR

unread,
Apr 15, 2019, 9:05:15 AM4/15/19
to Django users

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

BIJAL MANIAR

unread,
Apr 17, 2019, 7:15:40 AM4/17/19
to Django users

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 

BIJAL MANIAR

unread,
Jun 10, 2019, 5:26:19 AM6/10/19
to Django users

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:
Reply all
Reply to author
Forward
0 new messages