你如何迭代django中的列表

时间:2010-10-17 02:30:28

标签: django loops

这是一个示例视图代码

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 /

中的TypeError      

'function'对象不可迭代

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

感谢您提前输入任何内容!


来自django错误页面

   / now /

中的TypeError      

'function'对象不可迭代

     

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

     

'function'对象不可迭代

     

例外位置:     C:\ Python27 \ LIB \站点包\ Django的\核心\ urlresolvers.py   在解决方案中,第121行Python   可执行文件:C:\ Python27 \ python.exe   Python版本:2.7.0 Python路径:     ['C:\ Documents and   设置\ JohnWong \工作空间\ mysite的\ mysite的”,   'C:\ Documents and   设置\ JohnWong \工作空间\ mysite的”,   'C:\ Python27','C:\ Python27 \ DLLs',   'C:\ Python27 \ lib中',   'C:\ Python27 \ lib中\ lib中-TK',   'C:\ Python27 \ LIB \高原双赢',   'C:\ Python27 \ LIB \站点包',   'C:\窗口\ 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 \站点包\ Django的\核心\处理器\ 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   异常值:'function'对象是   不可迭代

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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中传递的变量(同样,确保有'request'而不是'reqest'来保持正确)

def link(request,current_time,link):

答案 2 :(得分: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 %}
相关问题