1

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>
jayuu
  • 443
  • 1
  • 4
  • 17

1 Answers1

1

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

Messaoud Zahi
  • 1,214
  • 8
  • 15
  • Yes you're right. I could have sworn that I tried this but I guess not properly. Thanks also for the link to the documentation. – jayuu Dec 04 '16 at 11:37