如何在 django 中迭代列表

发布于 2024-09-27 23:38:41 字数 2331 浏览 1 评论 0原文

这是一个示例视图代码

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 -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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天暗了我发光 2024-10-04 23:38:41

你正在尝试迭代这件事,对吧?

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:“函数”对象没有属性“标题”

You're trying to iterate over this thing, right?

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

Well, link is a function (you def'd it, remember?) so you can't just access title like that. That code will not work in Python. If you try this:

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

You'll also get an error:

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

So要识趣 2024-10-04 23:38:41

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

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

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:

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

and then:

{% for item in titles %}
    {{item}}
    {% if not forloop.last %} | {% endif %}
{% endfor %}
紧拥背影 2024-10-04 23:38:41

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

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

然后进行迭代:

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

等等。

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


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

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

(r'^now/

它应该类似于

(r'^articles/(?P<current_time>\(?P<link>)/

然后 来容纳你显然在 URL 中传递的变量(另外,请确保有“请求”而不是“请求”以保持一切正常)

def link(request,current_time,link):
, current_time, link),# that isn't a proper reference to the 'link' function, and it can't come second

它应该类似于


然后 来容纳你显然在 URL 中传递的变量(另外,请确保有“请求”而不是“请求”以保持一切正常)


, 'project_name.views.link'), #the second tuple element is the view function

然后 来容纳你显然在 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:

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

and then iterating like:

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

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 -

(r'^now/

It should be something like

(r'^articles/(?P<current_time>\(?P<link>)/

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)

def link(request,current_time,link):
, current_time, link),# that isn't a proper reference to the 'link' function, and it can't come second

It 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)


, 'project_name.views.link'), #the second tuple element is the view function

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 second

It 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文