Reputation: 3740
I have a model like this:
class Component(models.Model)
html_name = models.CharField(max_length=100) #example value: header_1.html
class MyModel(models.Model):
components = models.ManyToManyField(Component ...)
In my Django project I have the following structure.
root
----myapp
--------templates
------------myapp
----------------templates
--------------------default_template_1.html
----------------components
--------------------header_1.html
In my template(default_template_1.html) I would like to do something like this:
{% for applied_component in mymodel.components.all %}
{% with '../components/'|add:applied_component as component_template %}
{% include component_template %}
{% endwith %}
{% endfor %}
That gives me an error: TemplateDoesNotExist
. However if I extract that string inside component_template
and just hardcode it, then the include works fine. So it seems like it's something with the fact that it's a variable.
I also tried changing include to: {% include component_template|stringformat:"i" %}
But that gives me:
PermissionError at /app/event/1/
[Errno 13]
Upvotes: 2
Views: 92
Reputation: 5888
I use a template variable as the include string and for my system it works fine:
{% with settings.get_template as template %}
{% if template %}
<div class="setupFormWrapper">
<form action="{% url 'simple:setup:products:settings_set' selected.id %}" method="POST" id="settingsForm" class="form{{ settings.formClass }}">
{% csrf_token %}
{{ template }}
</form>
</div>
So what you're trying to do is definitely possible. Are you sure you're constructing the path properly and that the template engine has access / permissions to handle the template?
For findability in google: The answer was to use absolute paths instead of relative paths.
Upvotes: 1