【问题标题】:'int' object is not iterable on Django Template'int' 对象在 Django 模板上不可迭代
【发布时间】:2013-12-16 13:47:25
【问题描述】:

'int' object is not iterable 我的模板出现此错误,我明白,我认为这是什么意思 - 条件:{% elif User_No_Auth_Tld > 0 %} 不能允许for() 循环遍历结果({% for NotAuthDomain in User_No_Auth_Tld %}),但我尝试了:{% elif User_No_Auth_Tld %}(没有整数条件),但仍然得到相同的错误。

我在下面做错了什么?

谢谢

{% if not User_Tld_Entered  %}  <!-- show ADD TLD form -->      
    <hr />
    #form here
    <br />
    <h4>Verify your domain</h4>

{% elif User_No_Auth_Tld > 0 %}

    <h1>display unauth domains, with auth link</h1>
        {% for NotAuthDomain in User_No_Auth_Tld %}
            {{ NotAuthDomain }}
        {% endfor %}
{% elif User_Auth_Tld %}
   #form here.
{% endif %}

对于未来可能会看到这篇文章寻求帮助的用户: 我最终将User_No_Auth_Tld 的声明更改为不对数据库记录使用.count() 方法。

【问题讨论】:

    标签: python django for-loop conditional-statements


    【解决方案1】:

    这与条件无关,而与可迭代本身有关。
    确保User_No_Auth_Tld 不是int,而是listdict 或任何其他可迭代对象。

    【讨论】:

    • User_No_Auth_Tld 是从视图传递的变量。声明是 User_Tld_Entered = Tld.objects.filter(Client_id=request.user).count()
    • 正如我所说的——.count() 方法返回整数。
    • 这是否表明它不是int? (这是一组 db obj。来自 db 表)
    • 我删除了 .count() 并且一切正常。我猜 db 结果不是 INT 而是那些对象:)
    • 没问题。然后考虑接受答案。此外,您还想了解更多关于 Python 中的可迭代以及 PEP 的信息,因为您的代码风格看起来有点欠缺。
    猜你喜欢
    • 2012-08-10
    • 2015-09-10
    • 1970-01-01
    • 2013-08-02
    • 2012-06-21
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多