Django - passing flatpage.title variable as part of an include

207 Views Asked by At

I'm using Django flatpages and trying to pass the title of a flatpage as part of an html include.

{% block navbar %}
  {% include 'navbar.html' with active='{{flatpage.title}}' %}
{% endblock %}

This is so I can highlight the whereabouts in the navigation bar.

<ul class="nav navbar-nav">
  <li class="{% if active == 'home' %}active{% endif %}"><a href="{% url 'home' %}">Home</a></li>
  etc.
</ul>

It doesn't appear to render correctly. Whereas if I replace {{flatpage.title}} with a hard-coded value ie. 'home' it works just fine.

{% block navbar %}
  {% include 'navbar.html' with active='home' %}
{% endblock %}

Am I not able to do this?

I'm not clear on a way to debug Django templates to check for these values... The way I'm currently checking that the variable is passing the right value is simply to reference {{flatpages.title}} elsewhere, separately in the html - which appears to render the correct 'home' value I'd expect.

<div id="navbar" class="navbar-collapse collapse">
  <ul class="nav navbar-nav">
     <li class="{% if active == 'home' %}active{% endif %}"><a href="{% url 'home' %}">Home</a></li>
     {{flatpage.title}}
     etc.
  </ul>
</div>
1

There are 1 best solutions below

1
Messaoud Zahi On BEST ANSWER

You do not need to surround arguments in {{ }} brackets in template tags.

If it's a variable, not a string, then do not use "" quotes.

The following should work:

 {% block navbar %}
   {% include 'navbar.html' with active=flatpage.title %}
 {% endblock %}

see include section to more informations