E
E
Evgeny_A2021-07-13 21:23:36
Django
Evgeny_A, 2021-07-13 21:23:36

How to use the results of one method call in different places in the template?

Hello!

I use Django3 and its default template engine. I added a method to the model that generates a random number from 0 to 100. Calling the method three times in different places of the same template, I see that it generates three different numbers. It turns out that if the method contained a query to the database, then they would be executed three times? If yes, then how to write methods or how to call them in a template so that the code is executed once?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-07-13
@Evgeny_A

Call a method on the view and pass its result to the template context.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question