0

I work on a Django project

when user is authenticated, I can get information of this user (queries on database) stored in five differents tables: user -- profile -- table1 -- table2 -- table3

I would like to have access to these data in each template

I currently use context to pass data from views to template I also set session variables that I can use in views to set context

but doing like that, I must pass these data in each views as session variables can be directly access in template

but, is there a simpliest way of getting user data available in all template without having to pass context in each template?

hope my question is clear enough...

SLATER
  • 613
  • 9
  • 31
  • Does this answer your question? [Django - How to make a variable available to all templates?](https://stackoverflow.com/questions/17901341/django-how-to-make-a-variable-available-to-all-templates) – Pedram Parsian Dec 11 '19 at 16:42
  • yes, seems context processor perfect ! – SLATER Dec 11 '19 at 18:46

1 Answers1

0

I would suggest you checkout how to create middlewares on Django:

https://docs.djangoproject.com/en/3.0/topics/http/middleware/

A middleware allows you to execute certain actions on every request that goes into your server. You can create a middleware to add the user data on your request in order to have it available on all templates. Although be careful because this can and potentially make your web app slow, since it would be fetching this data on places you might not need it.

Pablo Estrada
  • 3,182
  • 4
  • 30
  • 74