2010-10-17 31 views
0

這是一個示例視圖代碼你如何遍歷一個列表在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

回答

0

這是關於你如何指定上下文的模板,我相信。請返回一本字典,而不是與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): 
+0

我想你和以前的海報的方法,我仍然得到同樣的錯誤。太奇怪了。 – CppLearner 2010-10-17 02:46:36

+0

@JohnWong您是否檢查過Django錯誤頁面的上下文/局部變量部分以查看它具有「list」或「title」的條目? – JAL 2010-10-17 02:49:42

+0

我對django相當陌生。旁邊的設置,其餘的貼(我更新了線程)。我沒有看到與列表有關的錯誤? – CppLearner 2010-10-17 02:55:37

0

您還沒有創建上下文。您正在傳遞需要上下文的列表,並在模板中使用視圖名稱。試試這個:

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 %} 
+1

如果你使用'render_to_response',你可以簡單地指定一個字典而不是一個上下文對象。 – JAL 2010-10-17 02:43:01

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'