I
I
Ivan2015-10-12 10:06:51
Django
Ivan, 2015-10-12 10:06:51

Value from the base on each page?

Good day, gentlemen.
It is necessary to transfer a value from the database to each page of the project. Tell me, please, is it possible to do this without setting the response of this value in each view? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Shultais, 2015-10-12
@ATNC

Good afternoon, yes, you can use Context Processors
1. Create your own Context Processor, like

from games.models import Game

def most_popular_games(request):
    return { "MOST_POPULAR_GAMES": Game.objects.filter(active=True).order_by("-rating", "-id")[:5] }

2. Connect it in settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
   ...
    "download_games.context_processors.most_popular_games",
    ...
}

3. Use a variable in any template, for example
{% for game in MOST_POPULAR_GAMES %}
    ...
{% endfor %}

S
sim3x, 2015-10-12
@sim3x

It is better to do this through CBV and simply pass a variable with data to the
context. Context processors are not a completely transparent way for such a simple action.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question