python - 'type' 类型的参数不可迭代

标签 python django django-templates typeerror

我编写了这个简单的方法,它似乎在终端中运行良好,但每次我在浏览器中启动它时,我都会得到:'type' 类型的参数不可迭代。

def get_main(request):
    t = get_template('main.html') 
    p = urllib2.urlopen("http://www.caltech.edu/news/rss.xml")
    x = minidom.parseString(p.read())
    titles = x.getElementsByTagName("title")
    items_list = []

    for title in titles:
        items_list.append(str(title.firstChild.nodeValue))

    subscriptions_list = ['Caltech']
    html = t.render(Context({'subscriptions_list': subscriptions_list, 'items_list': items_list}))
    return HttpResponse

HTML:

<body>
        <div class="wrap">
            <div class="banner">Infobesity</div>

            <!-- Subscriptions -->
                <div class="subscriptionsDiv">
                    <ol class="subscriptionsList">
                    {% for subscription in subscriptions_list %}
                        <li>{{ subscription }}</li>
                    {% endfor %}
                    </ol>
                </div>
            <!-- Subscriptions End -->

            <!--  Items -->
                <div class="itemsDiv">
                    <ol class="itemsList">
                    {% for item in items_list %}
                        <li>{{ item }}</li>
                    {% endfor %}
                    </ol>
                </div>

            <!-- Items End -->
        </div>
    </body>

回溯:

   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
      189.             response = self.apply_response_fixes(request, response)
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in apply_response_fixes
      237.             response = func(request, response)
    File "/usr/local/lib/python2.7/dist-packages/django/http/utils.py" in fix_location_header
      19.     if 'Location' in response and request.get_host():

最佳答案

你返回 HttpResponse 而没有调用它;这是一个类,因此不可迭代。

也许您打算在响应中返回 html

return HttpResponse(html)

关于python - 'type' 类型的参数不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14292292/

相关文章:

Python Mysql 查询缓存并在稍后使用它进行连接

python - 使用 "case"时获取空行(实际上是使用聚合时)

python - "Upload"来自 django shell 的文件

python - Django - 限制查看项目的用户

django - 更新模板内变量的值 - django

Django和crispy form,如何在crispy Layout中添加id和name

python - 使用 numpy 计算精度和准确度

Python:使用 Bottle 和 Tornado Web 服务器来处理并发连接

通过 Memcache 的 Django session : Cannot find session key manually

python - 通用外键或外键