TemplateSyntaxError: Invalid block tag

I made a template for my Django project but I’m getting a syntax error that I don’t know where it comes

Here is the error:
Invalid block tag on line 14: ‘endfor’, expected ‘endblock’. Did you forget to register or load this tag?

And here is my template code:

{% extends "base.html" %} {% block page_content %}
<div class="col-md-8 offset-md-2">
   <h1>Blog Index</h1>
   <hr />
   {% for post in posts %}
   <h2><a href="{% url 'blog_detail' post.pk%}">{{ post.title }}</a></h2>
      {{ post.created_on.date }} |&nbsp; Categories:&nbsp; {% for category in
      post.categories.all %}
      <a href="{% url 'blog_category' category.name %}"> {{ category.name }} </a
      >&nbsp; {% endfor %}
   <p>{{ post.body | slice:":400" }}...</p>
   {% endfor %}
{% endblock %}

If this is an accurate representation of the template, then you’ve got a couple of likely issues here.

First, you’re missing the space in post.pk%}.

Second, you’ve got the {% for category ... tag split across lines.

The template rendering engine is extremely picky when it comes to syntax of tags, filters, etc. Spaces and linefeeds are critical.

In particular, see #8652 (Multiline tags and tag escape tags) – Django.

1 Like