Django 模板 – 模板语法错误

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

django template -TemplateSyntaxError

问题

我是Django世界的初学者。当我尝试渲染我的login.html模板时,我遇到了这个问题,即使有一个关闭块。

TemplateSyntaxError at /login/
第12行的无效块标签:'else',期望'endblock'。您是否忘记注册或加载此标签?

这是我的模板

{% extends 'base.html' %}
{% block content %}
{% if not request.user.is_authenticated %}
<div style="margin-top: 30px">
  <form method="POST">
    {% csrf_token %} {% if error %}
    <p style="color: red">{{error}}</p>
    {% endif %}
    <div>
      <input type="text" name="username" placeholder="username" />
    </div>
    <div>
      <input type="password" name="password" placeholder="password" />
    </div>
    <button type="submit">Login</button>
  </form>
</div>

{% else %}

<p>
  You're already logged in. Would you like to <a href="/logout/"> logout?</a>
</p>
{% endif %}

{% 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

{% extends &#39;base.html&#39; %} 
{% block content %} 
{% if not request.user.is_authenticated %}
&lt;div style=&quot;margin-top: 30px&quot;&gt;
  &lt;form method=&quot;POST&quot;&gt;
    {% csrf_token %} {% if error %}
    &lt;p style=&quot;color: red&quot;&gt;{{error}}&lt;/p&gt;
    {% endif %}
    &lt;div&gt;
      &lt;input type=&quot;text&quot; name=&quot;username&quot; placeholder=&quot;username&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;input type=&quot;password&quot; name=&quot;password&quot; placeholder=&quot;password&quot; /&gt;
    &lt;/div&gt;
    &lt;button type=&quot;submit&quot;&gt;Login&lt;/button&gt;
  &lt;/form&gt;
&lt;/div&gt;

{% else %}

&lt;p&gt;
  You&#39;re already logged in. Would you like to &lt;a href=&quot;/logout/&quot;&gt; logout?&lt;/a&gt;
&lt;/p&gt;
{% endif %} 

{% 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:

确定