Reputation: 17050
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/$', 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
Upvotes: 0
Views: 1670
Reputation: 21563
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/$', current_time, link),# that isn't a proper reference to the 'link' function, and it can't come second
It should be something like
(r'^articles/(?P<current_time>\(?P<link>)/$', '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)
def link(request,current_time,link):
Upvotes: 0
Reputation: 360056
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'
Upvotes: 1
Reputation: 376052
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 %}
Upvotes: 0