如何在 django 中迭代列表
这是一个示例视图代码
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 %}
这是一个示例 url 代码
(r'^now/$', current_time, link),
但是,我收到错误
/now/ 出现类型错误
“函数”对象不可迭代
我知道这在 Python 中有效。那么如何在 django 中进行迭代呢?
感谢您提前提供任何意见!
来自 django 错误页面
/now/ 出现类型错误
“函数”对象不可迭代
请求方法:GET 请求URL: http://127.0.0.1:8000/now/ Django 版本:1.2.3 异常类型: 类型错误异常值:
“函数”对象不可迭代
异常位置: C:\Python27\lib\site-packages\django\core\urlresolvers.py 在resolve中,第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/ Django 版本: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” 在获取响应中 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 异常值:“函数”对象是 不可迭代
This is a sample view code
def link(reqest):
title = ['Home Page', 'Current Time', '10 hours later']
return render_to_response('time.html', title)
This is a sample template code
{% for item in title %}
{{item}}
{% if not forloop.last %} | {% endif %}
{% endfor %}
This is a sample url code
(r'^now/
However, I get an error
TypeError at /now/
'function' object is not iterable
I know this works in Python. How do you iterate in django, then?
Thank you for any input in advance!
from django error page
TypeError at /now/
'function' object is not iterable
Request Method: GET Request URL:
http://127.0.0.1:8000/now/ Django
Version: 1.2.3 Exception Type:
TypeError Exception Value:
'function' object is not iterable
Exception Location:
C:\Python27\lib\site-packages\django\core\urlresolvers.py
in resolve, line 121 Python
Executable: C:\Python27\python.exe
Python Version: 2.7.0 Python Path:
['C:\Documents and
Settings\JohnWong\workspace\mysite\mysite',
'C:\Documents and
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']
Server time: Sat, 16 Oct 2010
22:45:36 -0400
Environment:
Request Method: GET Request URL:
http://127.0.0.1:8000/now/ Django
Version: 1.2.3 Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages'] Installed
Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback: File
"C:\Python27\lib\site-packages\django\core\handlers\base.py"
in get_response
91. request.path_info) File
"C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
217. sub_match = pattern.resolve(new_path) File
"C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
121. kwargs.update(self.default_args)
Exception Type: TypeError at /now/
Exception Value: 'function' object is
not iterable
, current_time, link),
However, I get an error
TypeError at /now/
'function' object is not iterable
I know this works in Python. How do you iterate in django, then?
Thank you for any input in advance!
from django error page
TypeError at /now/
'function' object is not iterable
Request Method: GET Request URL:
http://127.0.0.1:8000/now/ Django
Version: 1.2.3 Exception Type:
TypeError Exception Value:'function' object is not iterable
Exception Location:
C:\Python27\lib\site-packages\django\core\urlresolvers.py
in resolve, line 121 Python
Executable: C:\Python27\python.exe
Python Version: 2.7.0 Python Path:
['C:\Documents and
Settings\JohnWong\workspace\mysite\mysite',
'C:\Documents and
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']
Server time: Sat, 16 Oct 2010
22:45:36 -0400Environment:
Request Method: GET Request URL:
http://127.0.0.1:8000/now/ Django
Version: 1.2.3 Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages'] Installed
Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')Traceback: File
"C:\Python27\lib\site-packages\django\core\handlers\base.py"
in get_response
91. request.path_info) File
"C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
217. sub_match = pattern.resolve(new_path) File
"C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
121. kwargs.update(self.default_args)Exception Type: TypeError at /now/
Exception Value: 'function' object is
not iterable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你正在尝试迭代这件事,对吧?
好吧,
link
是一个函数(您def
了它,还记得吗?),所以您不能像这样访问title
。该代码在 Python 中不起作用。如果您尝试这样做:您还会收到错误:
You're trying to iterate over this thing, right?
Well,
link
is a function (youdef
'd it, remember?) so you can't just accesstitle
like that. That code will not work in Python. If you try this:You'll also get an error:
你还没有建立上下文。您正在传递需要上下文的列表,并在模板中使用视图名称。试试这个:
然后:
You haven't made a context. You're passing a list where a context is needed, and using a view name in the template. Try this:
and then:
我相信,这与您如何指定模板的上下文有关。尝试返回一个字典,其中包含
title
:然后进行迭代:
等等。
请注意,循环中的
item
周围需要两个括号,而不是一个。现在您已经添加了额外的信息,我发现错误在视图执行之前就出现了(尽管到达那里后您也会遇到一些错误)。
URL 规范将可调用对象作为第二个参数。你那里有几个变量 -
然后 来容纳你显然在 URL 中传递的变量(另外,请确保有“请求”而不是“请求”以保持一切正常)
, current_time, link),# that isn't a proper reference to the 'link' function, and it can't come second它应该类似于
然后 来容纳你显然在 URL 中传递的变量(另外,请确保有“请求”而不是“请求”以保持一切正常)
This is about how you're specifying the context for the template, I believe. Try returning a dictionary instead, with
title
inside of it:and then iterating like:
etc.
Note that you need two brackets around
item
in the loop, rather than one.Now that you've added the extra info, I see the error is coming before the view is even executed (though you would have had a few once you got there, too).
The URL specification takes a callable as it's second argument. You have a couple of variables on there -
and then to accommodate the variables you are apparently passing in the URL, (also, make sure to have 'request' and not 'reqest' to keep things straight)
, current_time, link),# that isn't a proper reference to the 'link' function, and it can't come secondIt should be something like
and then to accommodate the variables you are apparently passing in the URL, (also, make sure to have 'request' and not 'reqest' to keep things straight)