【问题标题】:how do you iterate over a list in django你如何遍历 django 中的列表
【发布时间】:2011-04-26 11:46:14
【问题描述】:

这是一个示例视图代码

def link(reqest):
    title  = ['Home Page', 'Current Time', '10 hours later']
    return render_to_response('time.html', title)

这是一个示例模板代码

{% for item in title %}
    {{item}}
    {% if not forloop.last %} | {% endif %}
{% endfor %}

这是一个示例网址代码

(r'^now/$', current_time, link),

但是,我得到一个错误

/now/处的类型错误

'function' 对象不可迭代

我知道这适用于 Python。那么你如何在 django 中迭代呢?

感谢您提前提供任何意见!


来自 django 错误页面

/now/处的类型错误

'function' 对象不可迭代

请求方法:GET 请求 URL: http://127.0.0.1:8000/now/姜戈 版本:1.2.3 异常类型: TypeError 异常值:

'function' 对象不可迭代

异常位置: C:\Python27\lib\site-packages\django\core\urlresolvers.py 在解决中,第 121 行 Python 可执行文件:C:\Python27\python.exe Python 版本:2.7.0 Python 路径: ['C:\文档和 Settings\JohnWong\workspace\mysite\mysite', 'C:\文档和 Settings\JohnWong\workspace\mysite', 'C:\Python27', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\lib-tk', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\site-packages', 'C:\WINDOWS\system32\python27.zip'] 服务器时间:2010 年 10 月 16 日星期六 22:45:36 -0400

环境:

请求方法:GET 请求 URL: http://127.0.0.1:8000/now/姜戈 版本:1.2.3 Python 版本:2.7.0 已安装的应用程序: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages'] 已安装 中间件: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware')

追溯:文件 “C:\Python27\lib\site-packages\django\core\handlers\base.py” 在 get_response 91. request.path_info) 文件 解析中的“C:\Python27\lib\site-packages\django\core\urlresolvers.py” 217. sub_match = pattern.resolve(new_path) 文件 解析中的“C:\Python27\lib\site-packages\django\core\urlresolvers.py” 121. kwargs.update(self.default_args)

异常类型:/now/ 处的 TypeError 异常值:“功能”对象是 不可迭代

【问题讨论】:

    标签: django loops


    【解决方案1】:

    你正在尝试迭代这个东西,对吧?

    title  = ['Home Page', 'Current Time', '10 hours later']
    

    好吧,link 是一个函数(你 def 得到了它,记得吗?)所以你不能像那样访问 title。该代码在 Python 中不起作用。如果你试试这个:

    def link(reqest):
        title  = ['Home Page', 'Current Time', '10 hours later']
        return render_to_response('time.html', title)
    
    for item in link.title:
        print title
    

    你也会得到一个错误:

    AttributeError: 'function' object has no attribute 'title'

    【讨论】:

      【解决方案2】:

      您还没有创建上下文。您正在传递需要上下文的列表,并在模板中使用视图名称。试试这个:

      def link(request):
          c = Context()    
          c['titles'] = ['Home Page', 'Current Time', '10 hours later']
          return render_to_response('time.html', c)
      

      然后:

      {% for item in titles %}
          {{item}}
          {% if not forloop.last %} | {% endif %}
      {% endfor %}
      

      【讨论】:

      • 如果您使用render_to_response,您可以简单地指定字典而不是上下文对象。
      【解决方案3】:

      我相信这是关于您如何指定模板的上下文。尝试返回字典,其中包含 title

      return render_to_response('time.html', {"title":title})
      

      然后像这样迭代:

      {% for item in title %}
          {{ item }}
      

      等等

      请注意,循环中的item 周围需要两个括号,而不是一个。


      现在您已经添加了额外的信息,我发现在视图执行之前就出现了错误(尽管一旦您到达那里,您也会遇到一些错误)。

      URL 规范将一个可调用对象作为它的第二个参数。你有几个变量在那里 -

      (r'^now/$', current_time, link),# that isn't a proper reference to the 'link' function, and it can't come second
      

      应该是这样的

      (r'^articles/(?P<current_time>\(?P<link>)/$', 'project_name.views.link'), #the second tuple element is the view function
      

      然后为了适应您显然在 URL 中传递的变量,(另外,请确保有“请求”而不是“请求”以保持直截了当)

      def link(request,current_time,link):
      

      【讨论】:

      • 我尝试了你和之前发帖者的方法,我仍然得到同样的错误。太奇怪了。
      • @JohnWong 你检查过 Django 错误页面的上下文/局部变量部分,看看它对listtitle 有哪些条目?
      • 我对 django 还是很陌生。除了设置之外,其余的都已发布(我更新了线程)。我根本没有看到与列表相关的错误?
      • @JohnWong 好的,我正在更新我的帖子。
      • 在谷歌上搜索后,我认为这是从 URL 中捕获链接?我实现了它,但它仍然不起作用(当然我将它改为现在,而不是文章,并将 project_name 替换为 mysite(我的项目名称).....)我得到“没有匹配的 url”。我基本上有一个叫做 current_time 的函数,还有一个叫做 link 的函数。我有一个名为“time.html”的模板。我想在 time.html 上使用这两个视图函数。当然,我可以在单一视图功能中做到这一点。但一般来说,我们可能有第 n 个视图函数在同一个 html 上传递和使用。但是感谢您到目前为止的输入!!!
      猜你喜欢
      • 2022-06-18
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      相关资源
      最近更新 更多