How to set (global) context value from a templatetag?

131 views
Skip to first unread message

toinbis

unread,
Jul 28, 2019, 1:01:48 PM7/28/19
to Django users
Hi,

I wonder how would I access context and set the key/value pair from within custom template tag?

Later on I want to access context object in djang-debug-toolbar panel with 

from django.template import RequestContext; rc = RequestContext(request)

rc contains all the context that had been passed on to TemplateResponse within the view, o I want to add few more there from the template tag and just cant figure out how.

Thanks a lot for any ideas!

Tom

James Schneider

unread,
Jul 28, 2019, 2:42:02 PM7/28/19
to django...@googlegroups.com


from django.template import RequestContext; rc = RequestContext(request)

rc contains all the context that had been passed on to TemplateResponse within the view, o I want to add few more there from the template tag and just cant figure out how.

Thanks a lot for any ideas!

First thing that comes to mind:



If you'll be using these same variables in a majority of your templates, then I'd recommend looking at a custom template context processor:


That way you can define constants in you application code and have them automatically pulled in as template variables in all your templates without having to repeat a template tag in every template.

-James
Reply all
Reply to author
Forward
0 new messages