We had a problem like this, and the key gets the correct data in the Context. What we have done is split the data creation / context filling for each view into a separate build-the-context routine. Original views simply call their respective procedure and then display their template. The combined view calls each of the context creators, and then displays the main template, which then includes the subpatterns.
Here we are faced with a problem with the Django template system. We cached the template fragments, and some of these fragments took data that was very expensive to generate. If the fragment was not obsolete, we definitely did not want to do this work. But postponing the work until we find out what we need, this meant that we are now in the template and:
- You cannot pass parameters to methods from a template.
- Django.template .__ init __ method. Variable._resolve_lookup () was violated if you passed the called code, it would not have called it! If you reference an object method in context, this works just fine.
The reason for using called calls is that it allows you to pass a curried function โ that is, a function that already has some (or all) of its parameters, but which have not yet been called. Thus, the view (or constructing the context in the case) should be able to perform the full specified function (remember that you cannot pass parameters in the templates themselves) so that the template, when needed, can call the called, get the data, and we leave .
We took two separate approaches to this:
Since we made this site, I found out that we could solve it using generators as pending data producers. Generators act like a curried function (in which you can pass arbitrary parameters for installation), but the template engine treats them as just another iterator. There is an excellent tutorial on this. Note: generators are not arrays, and you can only use them once, so some of your logic elements may need to be changed.
Next time, I think, we just go to jinja2 templates and stop twisting with Django templates.
source share