I’m trying to test out the partial templates in Django 6, but running into errors I can’t figure out.
Here is index.html:
<p>No polls are available.</p>
{{ hello }}
<hr>
{% include "./partial.html" %}
<hr>
{% include "./partial.html#bar" %}
{% partial bar %}
And partial.html, sibling to index.html:
{% partialdef bar %}
Hello Partial
{% endpartialdef %}
partial.html
I’m getting error from the {% partial bar %} line:
TemplateSyntaxError at /polls/
Partial 'bar' is not defined in the current template.
If I remove that line it renders without errors.
If I place the partial definition in the same file as index.html it also renders without errors.
From what I can tell I’m doing what the documentation describes:
Template partials can also be included using the include template tag with the same # directive:
{% include "authors.html#user-info" %}
But, clearly I’m missing something. Any ideas to what I’m doing wrong?
Partial rendering using the partial tag can only used within the same template (as you have seen in regards to the errors)
The key thing with partials is that they can be referenced in template paths using the # symbol. So when using it in with the include tag it ought to render only the partial within the other file. The other place this is useful is specifying a partial when rendering a template from a view
Oh, I read the documentation as I could load common partials. hm… I had my own partials system that do that, but I thought I’d standardize of what Django provided out of the box. Seems like it wasn’t the overlap I expected.
Give that you can render a partial from a view I’m surprised you cannot load them into other template files.
You can render them in another using include with a hash, but otherwise the design is that the rendering is within the same template file.
If your interested in more historical discussion I would refer to the third party package where they originiated from Carlton Gibson