Issue with Django template 'ifequal' [clos

2019-10-06 17:32发布

The Below code:

{% ifequal username AnonymousUser %}
    <p>Welcome</p>
{% else %} 
    <p> Welcome {{ username }}. Thanks for logging in.</p>     
{% endifequal %}

Shows This:

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 comes from:

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.

标签: django
1条回答
够拽才男人
2楼-- · 2019-10-06 18:00

You need to compare to a string. Use this:

{% ifequal smart_str(username).strip() "AnonymousUser" %}

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.

查看更多
登录 后发表回答