tags:

views:

59

answers:

3

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

A: 

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):
Alex JL
I tried your and the previous poster's method, and I am still getting the same error. so weird.
JohnWong
@JohnWong have you inspected the context/local variables section of the Django error page to see what entries it has for `list` or `title`?
Alex JL
I am quite new to django. Beside the setting, the rest are posted (i updated the thread). I don't see an error related to list at all?
JohnWong
@JohnWong Okay, I'm updating my post.
Alex JL
after searching on google, i think that's capturing the link from URL? i implemented it and it still doesn't work (of course i changed it to now, instead of articles, and replace project_name with mysite (my project name)..... ) i am getting "no url matched". I basically had a function called current_time, and another one called link. I have one template called "time.html". And I want to use both view functions on the time.html. Of course I can do it in a single view function. But in general, we might have n-th view functions to pass and use on the same html. But thanks for ur input so far!!!
JohnWong
A: 

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 %}
Ned Batchelder
If you're using `render_to_response`, you can simply specify a dictionary rather than a context object.
Alex JL
+1  A: 

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'

Matt Ball