S
S
Saveli Tomak2017-08-22 19:23:30
Django
Saveli Tomak, 2017-08-22 19:23:30

Django: How to inject data from the database into each page (a la @inject in Laravel)?

Hello everyone, on each page in the footer there is a block with contacts. The contacts themselves are stored in the database. The first thing that comes to mind: in each view in the context, add a selection from the database by the contacts key, but if the number of views grows, then all this will become unmanageable. The second thought is to write your own context processor returning a dictionary consisting of a selection from the database, after which append it to the main context formed in the View and feed it to the template. There are also several questions here, is it correct to access the database from the context processor and then join them with the context formed in the View? Maybe there is a solution that floats somewhere in the documentation, where I have not rummaged through yet?
ps: for example, in the php laravel framework there is an @inject function built into the blade that allows you to access the ORM entity directly from the template.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-08-22
@aresouji

In the case of fbv - functional based views - make a function that returns the context and execute it in each view
CBV - ContextMixin
Less desirable, use the context processor
Very bad - tags

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question