Django比较模板中的变量不起作用(Django Compare variables in template not working)

编程入门 行业动态 更新时间:2024-10-21 05:38:07
Django比较模板中的变量不起作用(Django Compare variables in template not working)

所以它似乎是重复的,但没有一个答案可以解决我的问题。

所以我正在使用基于分类的视图。 在上下文中,我发送的东西为active_country

{{active_country}} <select class="country-link inputText span5"> <option value="all">All Countries</option> {% for country in countries %} <option value="{{ country.country_code }}" {% if active_country == country.name %} selected="selected" {% else %} not working {% endif %}>{{ country.name }}</option> {% endfor %} </select>

但if条件永远不会成真。 我甚至尝试过:

{% if active_country == "Australia" %} selected="selected" {% endif %}

活跃的国家给澳大利亚,但仍然没有得到真实的条件。

我也尝试过:

{% ifequal active_country "Australia" %} selected="selected" {% endifequal %}

我肯定知道active_country正在接收。 我也单独打印过。

任何人都可以提出错误吗?

So it may seem as a duplicate but none of the answers solve my issue.

So I'm using classed based views. In context I'm sending something as active_country

{{active_country}} <select class="country-link inputText span5"> <option value="all">All Countries</option> {% for country in countries %} <option value="{{ country.country_code }}" {% if active_country == country.name %} selected="selected" {% else %} not working {% endif %}>{{ country.name }}</option> {% endfor %} </select>

But the if condition is never getting true. I even tried:

{% if active_country == "Australia" %} selected="selected" {% endif %}

active country gives Australia but still condition does not get True.

And I have also tried:

{% ifequal active_country "Australia" %} selected="selected" {% endifequal %}

I for sure know the active_country is being received right. I have printed it separately as well.

Can any one suggest whats wrong?

最满意答案

所以我回答了我的一个问题。 我犯的错误:

active_country是一个对象。 因此我应该使用active_country.name

我感到困惑,因为{{active_country}}也给出了名字。 这是因为在我的models.py中将unicode设置为name

So I'm answering my one question. The error I was making:

active_country is an object. Hence I should be using active_country.name

I was getting all confused as just {{active_country}} was also giving the name. This was because the unicode was set to name in my models.py

更多推荐

本文发布于:2023-07-05 06:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   不起作用   模板   Django   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!