问题与Django模板'ifequal' [英] Issue with Django template 'ifequal'
问题描述
以下代码:
{% ifequal username AnonymousUser %}
<p>Welcome</p>
{% else %}
<p> Welcome {{ username }}. Thanks for logging in.</p>
{% endifequal %}
显示此:
Welcome AnonymousUser. Thanks for logging in.
什么?我不止一点我很确定我不需要提供额外的代码来了解我的问题。
What the? I'm more than a little miffed. I'm pretty sure i don't need to supply extra code for you to understand my problem.
我不认为这是一个不平等的问题。我有一个很好的处理方式。
I dont think it's an ifequal problem. I have a pretty good handle on that.
用户名来自:
username = request.user
这是否意味着此代码中的用户名不是字符串。我必须把它转换成一个字符串。
Does this mean username at this point in the code is not a string. Do i have to convert it to a string.
推荐答案
你需要比较一个字符串。使用这个:
You need to compare to a string. Use this:
{% ifequal smart_str(username).strip() "AnonymousUser" %}
这里是 Django文档检查与 ifequal
的相等性
Here's the Django documentation on checking equality with ifequal
.
确保您的变量是一个字符串,还有一个被修剪的前导和尾随空格。
Ensure your variable is a string, and one that's trimmed of leading and trailing whitespaces as well.
这篇关于问题与Django模板'ifequal'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!