How to combine a template from other processed templates?

I have a django pro1 project with several applications: app1, app2, app3, etc. I want to display some top-level template containing blocks from each application:

example_base_template.html:

[header /] [left nav bar]{{ app1 rendered template }}[/left nav bar] [right nav bar]{{ app2 rendered template }}[/right nav bar] [center section]{{ app1 main functionality template }}[/center section] [footer]{{ app3 rendered template }}{{ app4 rendered template }}[/footer] 

All of these application patterns are dynamic that use the database. How to do it in the most correct and elegant way? Or maybe the question is how to connect 4 different types to the same URL?

+4
source share
4 answers

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.

0
source

You can use the tag {% include%} . But that doesnโ€™t really help you. The best solution is to write a special inclusion tag with the necessary template and functionality.

You cannot (in a simple way) mix multiple views into one. Try using its pretty django solution.

+2
source

I did this by writing custom template tags for each application that I wanted to include. At first, my template tags just returned hard-coded html. Later, I discovered that tags can load their own template fragments. There was also a snippet that was a common content tag that worked very well.

+1
source

Many reusable applications (especially those selected in the Pinax project) are great examples of using custom template tags to insert content. James Bennett talk at DjangoCon 2008 can also help.

0
source

All Articles