英文:
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 '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%}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论