【问题标题】:inside for loop if condition not working in django如果条件在 django 中不起作用,则在 for 循环中
【发布时间】:2014-10-15 15:53:34
【问题描述】:

http://localhost.com:8000/dashboard/track_info/13/这个页面有下拉菜单,用户可以选择国家。如果用户返回同一页面..这里我想默认显示选定的国家。

我尝试了以下但遗憾的是没有工作......

<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

你能在这里发现错误吗? ?

【问题讨论】:

  • 您确定该值为 int 2 而不是字符串“2”
  • @StuartLeigh 我不知道.. 但流程是...| {{ country.id }}2 然后 request.POST['territory_code'] 将只选择 2 .. 最后我会将这个值保存到会话中 request.session[event_id]['territory_code_id']
  • 我想你会发现request.POST['territory_code'] 是一个字符串,尝试将int(request.POST['territory_code']) 设置为会话,看看是否能解决你的问题。
  • @StuartLeigh Ya ... 工作正常... make ans..

标签: python django django-templates


【解决方案1】:

在 cmets 中编写解决方案 -

您的国家/地区 ID 是一个整数 2,而您的地区代码是一个字符串 "2"。这就是平等测试不成功的原因。在将其添加到会话之前,您应该将您的区域代码转换为整数(即​​ int(request.POST['territory_code']) )。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多