如果条件在django中不工作,则返回内部循环 [英] inside for loop if condition not working in django
问题描述
http://localhost.com:8000/dashboard/track_info/13/
此页面有用户可以选择国家/地区的下拉菜单。如果用户返回到同一页面..在这里我想显示所选国家的默认。
http://localhost.com:8000/dashboard/track_info/13/
this page have the dropdown there user can select country. If user return to the same page.. here i want to display selected country in default.
我尝试过下面但可悲的是没有工作...
I tried with below but sadly not working...
<option value=""></option>
{% for country in country_list %}
<option value="{{ country.id }}" {% if country.id == get.territory_code_id %} selected="selected" {% endif %}>{{ country.country_name | safe }}</option>
{% endfor %}
get.territory_code_id
来自会话,值为2
你可以在这里发现错误。 ?
Can you spot the wrong here. ?
推荐答案
在评论中写下解决方案 -
Writing up the solution in the comments -
您的国家ID是一个整数 2
,而您的领土代码是一个字符串2
。这就是平等考验不成功的原因。在将其添加到会话之前,您应该将您的领土代码转换为整数(即 int(request.POST ['territory_code'])
)。
Your country id is an integer 2
, whereas your territory code is a string "2"
. That's why the equality test is not succeeding. You should cast your territory code to an integer (i.e., int(request.POST['territory_code'])
) before adding it to the session.
这篇关于如果条件在django中不工作,则返回内部循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!