這是一個示例視圖代碼你如何遍歷一個列表在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),
但是,我得到一個錯誤
TypeError at /now/
'function' object is not iterable
我知道這在Python中有效。那麼你如何在django中迭代?
感謝您提前提出任何意見!
從Django的錯誤頁面
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
我想你和以前的海報的方法,我仍然得到同樣的錯誤。太奇怪了。 – CppLearner 2010-10-17 02:46:36
@JohnWong您是否檢查過Django錯誤頁面的上下文/局部變量部分以查看它具有「list」或「title」的條目? – JAL 2010-10-17 02:49:42
我對django相當陌生。旁邊的設置,其餘的貼(我更新了線程)。我沒有看到與列表有關的錯誤? – CppLearner 2010-10-17 02:55:37