Invalid block tag

Asked

Viewed 47 times

1

I’m new in Django and I’m having a problem with the tags Template

My code:

{% load static %}
{% block content %}
   <div>
    {{ questao.afirmativa }}
  </div>
    {% alternativas = questao.get_alternativas %}
    <table>
      <tr>
        {% for alternativa in alternativas %}
          <td>{{alternativa}}</td>
        {% end for %}
      </tr>
    </table>
{% endblock content %}

The object questao is already in the context of view. The method get_alternativas generates a list of alternatives that I want to go through with the for and show on the page.

What am I doing wrong?

You’re making a mistake on that line:

{% alternativas = questao.get_alternativas %}

Error message:

"- Invalid block tag on line 7: 'alternatives', expected 'endblock'. You forgot to register or load this tag?"

1 answer

1


As tags {% ... %} are unique to the templates system commands Django (whatever comes embedded in it, whether using the Jinja2).

To create a new variable within the template, use the tag {% with ... %}:

{% with alternativas as questao.get_alternativas %}
<table>
  <tr>
    {% for alternativa in alternativas %}
      <td>{{alternativa}}</td>
    {% endfor %}
  </tr>
</table>
{% endwith %}

Incidentally the {% endfor %} is spelled wrong in your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.