Django 模板 – 模板语法错误

huangapple go评论89阅读模式
英文:

django template -TemplateSyntaxError

问题

  1. 我是Django世界的初学者。当我尝试渲染我的login.html模板时,我遇到了这个问题,即使有一个关闭块。
  2. TemplateSyntaxError at /login/
  3. 12行的无效块标签:'else',期望'endblock'。您是否忘记注册或加载此标签?
  4. 这是我的模板
  5. {% extends 'base.html' %}
  6. {% block content %}
  7. {% if not request.user.is_authenticated %}
  8. <div style="margin-top: 30px">
  9. <form method="POST">
  10. {% csrf_token %} {% if error %}
  11. <p style="color: red">{{error}}</p>
  12. {% endif %}
  13. <div>
  14. <input type="text" name="username" placeholder="username" />
  15. </div>
  16. <div>
  17. <input type="password" name="password" placeholder="password" />
  18. </div>
  19. <button type="submit">Login</button>
  20. </form>
  21. </div>
  22. {% else %}
  23. <p>
  24. You're already logged in. Would you like to <a href="/logout/"> logout?</a>
  25. </p>
  26. {% endif %}
  27. {% endblock content%}
英文:

I am a beginner in Django world. When I try render my login.html template I'm facing this problem even there is a closing block.

TemplateSyntaxError at /login/
Invalid block tag on line 12: 'else', expected 'endblock'. Did you forget to register or load this tag?`

Here is my template

  1. {% extends &#39;base.html&#39; %}
  2. {% block content %}
  3. {% if not request.user.is_authenticated %}
  4. &lt;div style=&quot;margin-top: 30px&quot;&gt;
  5. &lt;form method=&quot;POST&quot;&gt;
  6. {% csrf_token %} {% if error %}
  7. &lt;p style=&quot;color: red&quot;&gt;{{error}}&lt;/p&gt;
  8. {% endif %}
  9. &lt;div&gt;
  10. &lt;input type=&quot;text&quot; name=&quot;username&quot; placeholder=&quot;username&quot; /&gt;
  11. &lt;/div&gt;
  12. &lt;div&gt;
  13. &lt;input type=&quot;password&quot; name=&quot;password&quot; placeholder=&quot;password&quot; /&gt;
  14. &lt;/div&gt;
  15. &lt;button type=&quot;submit&quot;&gt;Login&lt;/button&gt;
  16. &lt;/form&gt;
  17. &lt;/div&gt;
  18. {% else %}
  19. &lt;p&gt;
  20. You&#39;re already logged in. Would you like to &lt;a href=&quot;/logout/&quot;&gt; logout?&lt;/a&gt;
  21. &lt;/p&gt;
  22. {% endif %}
  23. {% endblock content%}

答案1

得分: 1

问题似乎来自于csrf_token。您将其用作标签而不是变量。

{% csrf_token %}更改为{{ csrf_token }},问题应该会解决。

英文:

The issue seems to be from the csrf_token. You are using it as a tag instead of variable.

Change {% csrf_token %} to {{ csrf_token }} and it should fix the issue.

huangapple
  • 本文由 发表于 2023年2月8日 23:06:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定